链表的输入问题
题目是输入一个数n,再输入n个学生的学号,成绩并输出,大家帮忙看一下我这个代码哪里错了,我已经纠结了几天了,谢谢。
程序代码:#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct stu)
struct stu
{
int num;
int score;
struct stu *next;
};
int main()
{
int i,n;
struct stu *p1,*p2,*head;
scanf("%d",&n);
p1=p2=(struct stu *)malloc(LEN);
head=p1;
scanf("%d %d",&p1->num,&p1->score);
for(i=1;i<n;i++)
{
p1=(struct stu *)malloc(LEN);
scanf("%d %d",&p1->num,&p1->score);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
free(p1);
for(i=0;i<n;i++,head=head->next);
printf("%d\t%d\n",head->num,head->score);
}






