可能这是非常基本的,每个人都会对我大喊大叫,但我已经尝试修复了几个小时,无法再忍受了。我有这个结构
struct node
{
Key_Type element;
tree_ptr left, right;
};
我正在尝试使用 strdup 将一个词放入元素中:
newNode->element = strdup(word);
我知道它可能不起作用,因为我试图分配一个指向普通变量的指针,但我不知道如何解决这个问题。
Table insert(Key_Type word,Table root)
{
struct node *newNode = malloc(sizeof(struct node));
struct node *left = malloc(sizeof(struct node));
struct node *right = malloc(sizeof(struct node));
newNode = root->head;
//fprintf (stderr, "Hi\n");
while(newNode->element != NULL)
{
//printf("%s",word);
if(strcmp(word,newNode->element) == 0)
{
fprintf (stderr, "Hi\n");
return root;
}
while(strcmp(word,newNode->element) == -1)
{
//fprintf (stderr, "Hi\n");
newNode = newNode->left;
//fprintf (stderr, "Hi\n");
}//if
//fprintf (stderr, "Hi\n");
while(strcmp(word,newNode->element) == 1)
{
//fprintf (stderr, "Hi\n");
newNode = newNode->right;
//fprintf (stderr, "Hi\n");
}//else if
}
//createNode(word);
newNode->element = strdup(word);
newNode->left = left;
newNode->right = right;
//fprintf (stderr, "Hi\n");
//printf("%s",root->head->element);
return root;
}