动态链表建立与输出错误
建立动态链表,存储学生的学号和成绩并输出。代码如下:
#include<stdio.h>
#include<malloc.h>
struct student
{
long num;
float score;
struct student *next;
};
struct student *creat()
{
struct student *head,*p,*pb;
head=NULL;
p=pb=(struct student *)malloc(sizeof(struct student));
scanf("%ld,%f",&p->num,&p->score);
p->next=NULL;
while(p->num!=-1)
{
if(head==NULL)
head=p;
else
pb->next=p;
pb=p;
p=(struct student *)malloc(sizeof(struct student));
scanf("%ld,%f",&p->num,&p->score);
}
p->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *pp;
pp=head;
while(pp!=NULL)
{
printf("%ld%5.1f\n",pp->num,pp->score);
pp=pp->next;
}
}
void main()
{
struct student *creat();
void print(struct student *head);
struct student *head;
head=creat();
print(head);
}
运行时输入一组数据没问题,但输入两组以上就会发生如下错误:
请各位为我指点指点。