看下合并链表哪里出问题了
#include "stdio.h"#include "malloc.h"
#define null 0
#define len sizeof(struct student)
int n;
struct student
{
long num;
long score;
struct student *next;
};
struct student *creat(void)
{
struct student *p1,*p2,*head;
head=null;
printf("please input the students' data:\n");
p1=p2=(struct student *)malloc(len);
scanf("%ld %ld",&p1->num,&p1->score);
n=0;
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(len);
scanf("%ld %ld",&p1->num,&p1->score);
}
p2->next=null;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=null)
{
printf("%ld %ld\n",p->num,p->score);
p=p->next;
}
}
struct student *insert(struct student *head,struct student *stu)
{
struct student *p1,*p2,*p;
p1=p2=head;p=stu;
if(head==null)head=p;
else
{
while(p->num>p1->num&&p1->next!=null)
{
p2=p1;p1=p1->next;
}
if(p->num<=p1->num)
{
if(p==head)
{
head=p;p1->next=p;
}
else
{
p2->next=p;p->next=p1;
}
}
else
{
p1->next=p;p->next=null;
}
n++;
}
return(head);
}
main()
{
struct student *a,*b,*p1,*t1;
a=creat();
print(a);
b=creat();
print(b);
p1=a;t1=b;
while(p1!=null)
{
t1=insert(t1,p1);
p1=p1->next;
}
printf("the %d data:",n);
print(t1);
}