帮我检查一下我的单链表哪里有错

来源:百度知道 编辑:UC知道 时间:2024/07/04 02:53:54
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct stu
{
int num;
char name[20];
struct stu *next;
};
int main()
{
struct stu *previous=NULL;
struct stu *first=NULL;
struct stu *current=NULL;
char check;

for(;;)
{
printf("do your want to input info y or n?\n");
scanf("%c",&check);
printf("\n");
if(check!='y')
break;
current=(struct stu *)malloc(sizeof(struct stu));
if(first==NULL)
first=current;
if(previous!=NULL)
previous->next=current;

printf("input the num");
scanf("%d",current->num);
printf("input the name");
scanf("%s",current->name);

current->next=NULL;
previous=current;

}
current=first;

while(current!=NULL

这是我修改后的程序

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

struct stu
{
int num;
char name[20];
struct stu *next;
};
int main()
{
struct stu *previous=NULL;
struct stu *first=NULL;
struct stu *current;
char check;

for(;;)
{
printf("do your want to input info y or n?\n");
while(isspace(check=getchar()));
printf("\n");
if(check!='y')
break;
current=(struct stu *)malloc(sizeof(struct stu));
printf("input the num\n");
scanf("%d",¤t->num);
printf("input the name\n");
scanf("%s",current->name);
if(first==NULL)
first=current;
if(previous!=NULL)
previous->next=current;
current->next=NULL;
previous=current;

}
current=first;