简单的链表程序找错
原题:每个学生有3门课程结业考试成绩。输入每个学生的姓名和成绩,计算并输出每个学生的姓名和平均成绩。
我的程序:(错的)#include"stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
char name[20];
float score1;
float score2;
float score3;
struct student *next;
};
struct student *create()
{
int n;
n=1;
struct student *head,*p1,*p2;
p1=(struct student *)malloc(LEN);
head=NULL;
gets_s(p1->name);
scanf("%f%f%f",&p1->score1,&p1->score2,&p1->score3);
p1->next=NULL;
while(p1->score1>=0)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
gets_s(p1->name);
scanf("%f%f%f",&p1->score1,&p1->score2,&p1->score3);
p1->next=NULL;
}
free(p1);
return(head);
}
void main(struct student *create)
{
float average;
struct student *p,*head;
p=create;
head=create;
while(p->next!=NULL)
{
if(head==NULL)printf("\nlist NULL\n");
else
{
average=(p->score1+p->score2+p->score3)/3;
printf("\nName:%s\nAverage:%f\n",p->name,average);
p=p->next;
}
}
}