我正在使用c中的双向链表制作树。我在该函数中使用递归调用,但不知何故它不起作用。我的代码是:
struct node
{
int data;
struct node *right;
struct node *left;
};
struct node* getNode()
{
struct node *temp;
temp= (struct node *)malloc(sizeof(struct node));
temp->right=NULL;
temp->left=NULL;
return temp;
}
在下面的函数中,我遇到了问题。
struct node* maketree()
{
struct node *t;
t=getNode();
int value;
char choice1='n',choice2='n';
printf("\nenter the value to the node");
scanf("%d",&value);
t->data=value;
printf("\nis there any left child??\n");
scanf("%c",&choice1); // I think here my problem is .
if (choice1 == 'y')
{
t->left=maketree();
}
printf("\nis there any right child??\n");
scanf("%c",&choice2);
if (choice2 == 'y' || choice2 == 'Y')
{
t->right=maketree();
}
return t;
}
int main (void)
{
struct node *t;
t=maketree();
return;
}
代码编译正确,但问题是,代码不等待我的选择(我使用scanf()
,C 应该等到我输入终端的输入。)但输出是:
enter the value to the node4
is there any left child??
is there any right child??
请协助。