私はリンクされたリストを初めて使用しますが、ノードの作成にほとんど問題がありません。
ここで、リンクされたリストの最初のノードを設定できましたが、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;
}
}