Codeup——607 | 问题 B: C语言-链表排序

题目描述

已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。

输入

第一行,a、b两个链表元素的数量N、M,用空格隔开。 接下来N行是a的数据 然后M行是b的数据 每行数据由学号和成绩两部分组成

输出

按照学号升序排列的数据

样例输入

2 3
5 100
6 89
3 82
4 95
2 10

样例输出

2 10
3 82
4 95
5 100
6 89
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct student{
	int sno;
	int score;
	struct student *next;
};

int main()
{
	int m,n,i,j;
	struct student *h,*pre,*p;
	cin >>m>>n;
	h=NULL;
	for(i=0;i<m+n;i++){	//将两个链表接在一起
		p=new student;
		cin >>p->sno>>p->score;
		p->next=NULL;
		if(!h)
			h=p;
		else
			pre->next=p;
		pre=p;
	}
	for(i=0;i<m+n;i++){	//排序
		p=h;
		for(j=0;j<m+n-1-i;j++){
			if(p->sno>p->next->sno){	//交换信息
				int temp1=p->sno;
				p->sno=p->next->sno;
				p->next->sno=temp1;
				int temp2=p->score;
				p->score=p->next->score;
				p->next->score=temp2;
			}
			p=p->next;
		}
	}
	p=h;
	while(p){	//输出
		printf("%d %d\n",p->sno,p->score);
		p=p->next;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值