链表出了点问题,实在找不出bug在哪里了,帮帮指点一下吧
程序代码:# include <stdio.h>
# include <stdlib.h>
# define LEN sizeof(struct Student)
typedef struct Student{
int num;
float score;
struct Student *pnext;
}NODE;
int n=0;
NODE*CreatList(void)
{
NODE*phead=NULL;
NODE*p1,*p2;
int t_num;
//得到一个t_num的合法值
while(1!=scanf("%d",&t_num))
{
fputs("输入错误\n",stderr);
int ch;
while((ch=getchar())!=EOF && ch!='\n')
continue;
}
//判断t_num是否为整数
while(t_num>0)
{
n++;
if(n==1)
{
p1=(NODE*)malloc(LEN); //p1分配空间
p2=p1;
}
else
p1=(NODE*)malloc(LEN); //p1分配空间
//检查p1是否分配成功
if(p1==NULL)
{
fputs("内存分配发生错误!\n",stderr);
exit(1);
}
//补全结构体信息
p1->num=t_num;//临时值补到p1
scanf("%f",&p1->score);
if(phead==NULL)
phead=p1;
else
p2->pnext=p1;
p2=p1;
while(1!=scanf("%d",&t_num))
{
fputs("输入错误\n",stderr);
int ch;
while((ch=getchar())!=EOF && ch!='\n')
continue;
}
}
p1->pnext=NULL;
}
int main (void)
{
NODE *p;
p=CreatList();
while(p!=NULL)
{
printf("%d %.3f",p->num,p->score);
p=p->pnext;
}
//这里只能输出最后一个节点的信息,why?
return 0;
}









