解题思路:创建链表,合并链表,排序,输出
注意事项:
参考代码:
#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语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:481 |
不容易系列 (C语言代码)浏览:702 |
拆分位数 (C语言代码)浏览:1361 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:634 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:818 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:913 |
IP判断 (C语言代码)浏览:592 |
快速排序算法1浏览:996 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:546 |
简单的a+b (C语言代码)浏览:587 |