新手请教:把一个链表插入到另一个链表中,写好的程序运行不了.谢谢
#include<stdio.h>struct stu
{
long num;
float score;
struct stu *next;
};
struct stu * insert(struct stu *head1,struct stu *head2) //两表都不为空,把head2表插入到head1中,按num的大小插入到合适的位置上,最后形成新的链表.
{
struct stu *p1,*p2,*pnew; //p1,p2用于head1中,pnew用于head2中
pnew=head2;
while(pnew!=NULL)
{ p1=head1;
while((pnew->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(pnew->num<=p1->num)
{
pnew->next=p1;
if(p1==head1) head1=pnew;
else p2->next=pnew;
}
else
{
p1->next=pnew;
pnew->next=NULL;
}
pnew=pnew->next;
}
return head1;
}
int main()
{
struct stu *head3,*head4,*p,*p0,*qp=NULL;
struct stu a,b,c,d,e,f;
a.num=1002;a.score=80;
b.num=1004;b.score=76;
c.num=1005;c.score=92;
d.num=1001;d.score=88;
e.num=1003;e.score=70;
f.num=1006;f.score=69;
head3=&a;a.next=&b;b.next=&c;c.next=NULL;
head4=&d;d.next=&e;e.next=&f;f.next=NULL;
p=head3;p0=head4;
while(p)
{
printf("%ld %5.1f\n",p->num,p->score); //先输出head1表
p=p->next;
}
while(p0)
{
printf("%ld %5.1f\n",p0->num,p0->score); //再输出head2表
p0=p0->next;
}
qp=insert(head3,head4); //这个有问题,请帮忙看下.
while(qp)
{
printf("%ld %5.1f\n",qp->num,qp->score); //再输出两表按num从小到大合并好的表
qp=qp->next;
}
printf("sucsess!\n");
return 0;
}