用单链表建立学生成绩管理系统
看小甲鱼的视频用单链表的方法建立学生成绩管理系统,代码没有错误,只能输入一次,发现第一次输入后在第一个while的判断中p1->num的值为0:
大佬看一下代码哪里有问题:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct nam)
struct nam *creat();
void prif(struct nam *head);
struct nam
{
int num;
float score;
struct nam *next;
};
int n;
void main()
{
struct nam *stu;
stu=creat();
prif(stu);
printf("\n\n");
system("pause");
}
struct nam *creat()
{
struct nam *head;
struct nam *p1,*p2;
p1=p2=(struct nam *)malloc(LEN);
printf("Please enter the num:");
scanf("%d",&p1->num );
printf("Please enter the score:");
scanf("%f",&p1->score );
head=NULL;
n=0;
while(p1->num);
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next =p1;
}
p2=p1;
p1=(struct nam *)malloc(LEN);
printf("\nPlease enter the num:");
scanf("%d",&p1->num );
printf("Please enter the score:");
scanf("%f",&p1->score );
}
p2->next =NULL;
return head;
}
void prif(struct nam *head)
{
struct nam *p;
printf("\nThere are %d records!\n",n);
p=head;
if(head)
{
do
{
printf("%d %2.1f\n",p->num ,p->score );
head =head->next ;
}
while(p);
}
}