题目,建立a,b两条链表,每个结点包括学号,成绩.要合并两条链表,按学号升序排列.
请各位师兄帮我看我的答案哪里出错了,我找了很久都找不出.
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define TURE 1
#define LEN sizeof(struct student)
struct student
{
int num;
int score;
struct student *next;
};
int n;
struct student *cread(void)
{
struct student *head,*p1,*p2;
n=0;
head=NULL;
while(TURE)
{
p1=(struct student*)malloc(LEN);
n++;
cout<<"please iput the numble:";
cin>>p1->num;
cout<<"score:";
cin>>p1->score;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
if(p1->num==0)
{
p2->next=NULL;
break;
}
}
return(head);
}
struct student *insert(struct student *head_a,struct student *head_b)
{
struct student *a1,*a2,*b;
a1=head_a;
b=head_b;
do
{
while((b->num>a1->num)&&(a1->next!=NULL))
{
a2=a1;
a1=a1->next;
}
if(b->num<=a1->num)//插入前面或中间
{
if(head_a==a1)
head_a=b;
else
a2->next=b;
b->next=a1;
a2=b;
}
else//插入链表最后
{
a1->next=b;
break;
}
}
while((b=b->next)!=NULL);
return(head_a);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
cout<<"numble:"<<p->num;
cout<<endl;
cout<<"score:"<<p->score;
cout<<endl;
p=p->next;
}while(p->next!=NULL);
}
int main()
{
struct student *a;
struct student *b;
struct student *c;//指向合并后的链表
a=cread();
b=cread();
c=insert(a,b);
print(c);
return 0;
}