1052:C语言程序设计教程(第三版)课后习题11.8

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值