输入两个单向链表并输出第二个链表
#include<stdio.h>#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n=0;
void main()
{
struct student *creat(void);
void print();
struct student *heada,*headb;
printf("input list a;\n");
heada=creat();
printf("input list b:\n");
headb=creat();
print(headb);
getch();
}
struct student *creat()
{
struct student *head,*p1,*p2;
printf("input number&score of student:\n");
p1=p2=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p2->score);
head=NULL;
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head==NULL)
printf("the empty list!\n");
else
while(p!=NULL)
{
printf("%d %4.2f\n",p->num,p->score);
p=p->next;
}
}
求高人指点一下,为什么第二次调用creat()函数执行错误?是不是只能建立一个链表呢?
谢谢了