解题思路:创建链表,合并链表,排序,输出
注意事项:
参考代码:
#include<iostream>
using namespace std;
struct student
{
int id, sum;
student *next;
};
student *Clist(int n) //创建链表
{
int idd,num;
student *head = NULL, *pnew = NULL, *p = NULL; //头指针,新节点指针,当前节点指针
cin >>idd>> num;
head = new student;
head->id = idd;
head->sum = num;
head->next = NULL;
p = head;
for (int i = 2; i <= n; i++)
{
cin >> idd >> num;
pnew = new student;
pnew->id = idd;
pnew->sum = num;
pnew->next = NULL;
p->next = pnew;
p = pnew;
}
return head;
}
student *Hblist(student *h1, student *h2,int n) //合并链表,排序
{
student *p = h1;
while (p->next)
p = p->next;
p->next = h2; //合并完成
student *pmin = h1, *pd = h1;
for (int i = 0; i < n - 1; i++) //对数据域的学号进行选择排序
{
p = pd->next;
while (p)
{
if (pmin->id > p->id)
pmin = p;
p = p->next;
}
if (pd != pmin) //交换数据域,使其学号升序
{
int d = pd->id;
int s = pd->sum;
pd->id = pmin->id;
pd->sum = pmin->sum;
pmin->id=d;
pmin->sum=s;
}
pd = pd->next;
pmin = pd;
}
return h1;
}
void SCList(student *head) //输出学号 ,成绩
{
student *p = head;
while (p)
{
cout << p->id << " " << p->sum << '\n';
p = p->next;
}
}
int main()
{
int a, b, n;
cin >> a >> b;
n = a + b;
student *head1 = Clist(a);
student *head2 = Clist(b);
head1= Hblist(head1, head2,n);
SCList(head1);
system("pause");
return 0;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复