求助,有两个链表,每个链表中的结点包括学号和成绩,要求把两个链表合并,按学号升序排列,请大家指出下面程序的错误之处
#include<iostream.h>class link
{
public:
int i,j;
link *next;
};
class linklist
{
protected:
link *p;
public:
link *hcreat()
{
int i,j;
link *s,*p;
p=new link;
p->next=NULL;
cin>>i>>j;
while(i!=0)
{
s=new link;
s->i=i;s->j=j;
s->next=p->next;
p->next=s;
cin>>i>>j;
}
return p;
}
void print(link *head)
{
link *q;
q=head->next;
while(q->next!=NULL)
{
cout<<q->i<<" "<<q->j<<endl;
q=q->next;
}
cout<<q->i<<" "<<q->j<<endl;
}
link hebin(link *head1,link *head2)
{
link *p,*q,*r,*head3;
p=head1->next;
q=head2->next;
head3=head1;
head3->next=NULL;
r=head3;
while((p->next!=NULL)&&(q->next!=NULL))
{
if(p->i>q->i)
{
r=p;
p=r->next;
}
else
{
r->next=q;
r=q;
q=r->next;
r->next=p;
}
if(p->next=NULL)
p->next=q;
else
q->next=p;
}
return *head3;
}
};
void main()
{
link *head1,*head2,*head3;
linklist a,b;
cout<<"输入第一组学生的学号和成绩:"<<endl;
head1=a.hcreat();
a.print(head1);
cout<<"输入第二组学生的学号和成绩:"<<endl;
head2=b.hcreat();
a.print(head2);
*head3=a.hebin(head1,head2);
cout<<"输出结果:"<<endl;
a.print(head3);
}