刚学习完链表,做个练习,输入时出现问题
#include<stdio.h>#include<stdlib.h>
int main(void)
{
struct Infor
{
int num;
char name[10];
int age;
struct Infor *next;
};
struct Infor people;
struct Infor *first,*now,*last;
first = now = last = NULL;
char test;
for(;;) //输入信息
{
if(first == NULL)
first = now;
if(last != NULL)
last->next = now;
printf("请输入编号:\n");
scanf("%d",&people.num);
printf("请输入名字:\n");
scanf("%s",people.name);
printf("请输入年龄:\n");
scanf("%d",&people.age);
now = (struct Infor*)malloc(sizeof(people));
if(malloc == NULL)
exit(1);
now = &people;
now->next = NULL;
last = now;
printf("继续输入信息?(Y/N)\n");
scanf("%c",&test);
if(test == 'n')
break;
}
now = first;
while(now != NULL) //输出信息
{
printf("%d号是%s,,%d岁\n",now->num,now->name,now->age);
last = now;
now = now->next;
free(last);
}
return 0;
}
为什么系统在打印“是否继续输入”后直接跳过判断而转向输入下一组数据??