1052: C语言程序设计教程(第三版)课后习题11.8
Description
已有a、b两个链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学号升序排列。
Input
第一行,a、b两个链表元素的数量N、M,用空格隔开。 接下来N行是a的数据 然后M行是b的数据 每行数据由学号和成绩两部分组成
Output
按照学号升序排列的数据
Sample Input
2 3
5 100
6 89
3 82
4 95
2 10
Sample Output
2 10
3 82
4 95
5 100
6 89
———-代码块
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct stu
{
int xh;
int score;
struct stu *next;
}stu;
int main()
{
int N,M;
cin>>N>>M;
stu *head,*a,*b;
head=(stu*)malloc(sizeof(stu));
head->next=NULL;
for(int i=0;i<N;i++)
{
a=(stu*)malloc(sizeof(stu));
cin>>a->xh>>a->score;
a->next=head->next;
head->next=a;
}
for(int i=0;i<M;i++)
{
b=(stu*)malloc(sizeof(stu));
cin>>b->xh>>b->score;
b->next=head->next;
head->next=b;
}
stu *q,*p;
head=head->next;
for(p=head;p;p=p->next)
for(q=p;q;q=q->next)
if(p->xh>q->xh)
{
int temp=p->xh;
p->xh=q->xh;
q->xh=temp;
temp=p->score;
p->score=q->score;
q->score=temp;
}
while(head)
{
cout<<head->xh<<' '<<head->score<<endl;
head=head->next;
}
free(head);
}