我是链表的新手,现在我在节点数量方面几乎没有问题。
在这里,我可以填充链表的第一个节点,但该gets()
函数似乎没有暂停执行以填充下一个节点。
输出如下:
Var name : var
Do you want to continue ?y
Var name : Do you want to continue ? // Here I cannot input second data
这是我的代码:
struct data
{
char name[50];
struct data* next;
};
struct data* head=NULL;
struct data* current=NULL;
void CreateConfig()
{
head = malloc(sizeof(struct data));
head->next=NULL;
current = head;
char ch;
while(1)
{
printf("Var name : ");
gets(current->name); //Here is the problem,
printf("Do you want to continue ?");
ch=getchar();
if(ch=='n')
{
current->next=NULL;
break;
}
current->next= malloc(sizeof(struct data));
current=current->next;
}
}