建立链表的问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 12:17:58
建立一个动态链表
包括若干学生的数据(学号,姓名,性别,年龄,成绩,住址)
输入这些数据,当输入END时结束.
struct student *input(void)
{
int flag=1;
char check[10]="end";
struct student *head=NULL;

while(flag){
p1=(struct student *)malloc(sizeof(struct student));
scanf("%s%s%s%d%f%s",p1->num,p1->name,p1->sex,&p1->age,&p1->score,p1->add);
if(strcmp(p1->num,check)==0)break;
if(head==NULL) head=p1,p2=p1;
else
{
p2->next=p1;
p2=p1;
}
}
p2->next=NULL;
return(head);
}
这是我编的一段函数,但是中间控制输入的环节有问题,该如何改进,谢谢

p1,p2没定义。
struct student *input(void)
{
int flag=1;
char check[10]="end";
struct student *head=NULL,*p1=NULL,*p2=NULL;

while(flag){
p1=struct student *)malloc(sizeof(struct student));
scanf("%s%s%s%d%f%s",p1->num,p1->name,p1->sex,&p1->age,&p1->score,p1->add);
if(strcmp(p1->num,check)==0)break;
if(head==NULL) head=p1,p2=p1;
else
{
p2->next=p1;
p2=p1;
}
}
p2->next=NULL;
return(head);
}