入门必做,动态链表,不知道哪里有问题
入门必做,动态链表,不知道哪里有问题。以下程序编译无错,但是运行时第一次输入数据后,提示debug error.
高手帮忙改下,并解释下啊
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "malloc.h"
#define null 0
#define len sizeof(struct student)
struct student /*定义结构体*/
{long num; float score;struct student *next;};
int n;
struct student *creat(void)/*定义建立动态链表的函数*/
{ struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(len);
printf("please input then student's date:\n");
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(len);
printf("please input then student's date:\n");
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=null;
return(head);
}
main()
{struct student *head;
head=creat();
}