2017-03-28 81 views
0

我試圖在二叉搜索樹中插入字符串。函數中的雙指針

那麼我要努力,從一個文件

解析字符串(含指令集),然後在功能

insertOpcodeFromFile()插入。

所以這個功能將執行

(*節點)= Node_insert(&節點,指令)。

該節點將是位於主函數中的二叉樹的根。

所以用簡單的方式來解釋,我想通過在其他函數中使用雙指針操作(插入)主函數中的根指針,其中包含插入函數。

我對指針有一個簡單的理解,但在這種情況下,我需要使用多於兩個指針,我認爲。

請使用此示例清楚地說明雙指針。

這裏是我的代碼(我註釋掉insert_node)

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#ifndef BINARYTREE_H_ 
#define BINARYTREE_H_ 

typedef struct node *NodePtr; 

typedef struct node { 
    char *word; 
    int  count; 
    NodePtr left; 
    NodePtr right; 
    } Node; 

NodePtr Node_alloc(); 
NodePtr Node_insert(NodePtr node_ptr, char *word); 
void clearArray(char a[]); 
void insertOpcodeFromFile(FILE *opcodeFile, NodePtr *node); 

void Node_display(NodePtr); 
char *char_copy(char *word); 

#endif 



int main(int argc, const char * argv[]) { 

FILE * opFile; 
FILE * progFile; 
struct node *root = NULL; 


if (argc != 4) {    // # of flag check 
    fprintf(stderr, " # of arguments must be 4.\n"); 
    exit(1); 
} 

opFile = fopen (argv[1], "r"); 
if(opFile == NULL) 
     { 
      fprintf(stderr,"There is no name of the opcode file\n"); 
      exit(1); 
     } 
progFile = fopen (argv[2], "r"); 
if(progFile == NULL) 
    { 
      fprintf(stderr,"There is no name of the program file \n"); 
      exit(1); 
    } 

insertOpcodeFromFile(opFile, &root); 
//Node_display(root); 

}/* main is over */ 

void insertOpcodeFromFile(FILE *opcodeFile, NodePtr *node) 
{ 
int fsize = 0; 
int lengthOfInst = 0; 
int c; 
int i; 
char buffer[100]; 
fsize = getFileSize(opcodeFile); 
enum flag {ins,opc,form}; 
int flag = ins; 
char instruction[6]; 
unsigned int opcode = 0; 
unsigned char format; 
while (c != EOF) 
{ 
    c = fgetc(opcodeFile); 
    buffer[i++] = c; 

    if (c == 32){ 
     switch (flag) { 

     case ins: 
      flag = opc; 

      memcpy(instruction,buffer,i); 
      instruction[i] = '\0'; 
      clearArray(buffer); 
      i = 0; 
      // printf("인스트럭션 : %s\n",instruction); 
      break; 

     case opc: 
      flag = form; 
      opcode = atoi(buffer); 
      clearArray(buffer); 
      i = 0; 
      // printf("옵코드 : %d\n",opcode); 
      break; 

     default: 
      break; 
     }/* end of switch */ 
    }/* end of if(space) */ 
    if((c == 10) || (c == EOF)) 
    { 
     if (flag == form) 
     { 
      format = buffer[0]; 
      clearArray(buffer); 
      i = 0; 
      // printf("포멧: %c\n", format); 

     } 
     flag = ins; 
     //node = Node_insert(node,instruction); 


    } 
} 
//Node_display(node); 
} 
int getFileSize(FILE *opcodeFile) 
{ int fsize = 0; 
fseek(opcodeFile,0, SEEK_SET); 
fseek(opcodeFile,0, SEEK_END); 
fsize = (int)ftell(opcodeFile); 
fseek(opcodeFile,0, SEEK_SET); 
return fsize; 
} 
int countUntilSpace(FILE *opcodeFile, int currentPosition) 
{ char readword[1]; 
char *space = " "; 
char *nextLine = "/n"; 
int i = 0; 
//printf("현재: %d\n",currentPosition); 
while(1) 
{ 
    fread(readword, sizeof(char),1,opcodeFile); 
    i++; 

    if(strcmp(readword,space) == 0 || strcmp(readword,nextLine) == 0) 
    { 
     //printf("break\n"); 
     break; 
    } 
} 

fseek(opcodeFile,currentPosition ,SEEK_SET); 
//printf("끝난 현재 :%d\n",ftell(opcodeFile)); 
//printf("%I : %d\n",i); 
return i - 1; 
} 
void clearArray(char a[]) 
{ 
memset(&a[0], 0, 100); 
} 

NodePtr Node_alloc() 
{ 
return (NodePtr) malloc(sizeof(NodePtr)); 
} 

NodePtr Node_insert(NodePtr node_ptr, char *word) 
{ 
int cond; 

if (node_ptr == NULL) { 
    node_ptr = Node_alloc(); 
    node_ptr->word = char_copy(word); 
    node_ptr->count = 1; 
    node_ptr->left = node_ptr->right = NULL; 
} else if ((cond = strcmp(word, node_ptr->word)) == 0) { 
    node_ptr->count++; 
} else if (cond < 0) { 
    node_ptr->left = Node_insert(node_ptr->left, word); 
} else { 
    node_ptr->right = Node_insert(node_ptr->right, word); 
} 
return node_ptr; 
} 

void Node_display(NodePtr node_ptr) 
{ 
if (node_ptr != NULL) { 
    Node_display(node_ptr->left); 
    printf("%04d: %s\n", node_ptr->count, node_ptr->word); 
    Node_display(node_ptr->right); 
} 
} 

char *char_copy(char *word) 
{ 
char *char_ptr; 

char_ptr = (char *) malloc(strlen(word) + 1); 
if (char_ptr != NULL) { 
    char_ptr = strdup(word); 
} 
return char_ptr; 
} 
+3

你的問題,特別是你的代碼很長。請考慮將其減少到20行或者人們不太可能讀取它。嘗試單獨解決問題並刪除已按預期工作的任何內容。 –

回答

0

在這種情況下,在main()

Node *root; 

爲什麼你需要使用一個 「雙」 指針(Node **)在改變root的函數是因爲root的值在這些函數中設置。

例如,假設您想分配一個Node並將其設置爲root
如果做以下

void alloc_root(Node *root) { 
    root = malloc(sizeof (Node)); 
    // root is a function parameter and has nothing to do 
    // with the 'main' root 
} 
... 
    // then in main 
    alloc_root(root); 
    // here main's root is not set 

使用指針的指針(即你所說的「雙指針」)

void alloc_root(Node **root) { 
    *root = malloc(sizeof (Node)); // note the * 
} 
... 
    // then in main 
    allow_root(&root); 
    // here main's root is set 

的混亂從主要的Node *rootroot來可能是一個指針一個Node。你將如何在函數f中設置一個整數int i;?您可以使用f(&i)調用函數f(int *p) { *p = 31415; }i設置爲值31415

考慮root是一個變量,它包含一個地址到Node,並在一個函數中設置它的值,您必須通過&rootrootNode *,這使得另一個*,如func(Node **p)