公统纯冠希


私信TA

用户名:dotcpp0623670

访问量:87

签 名:

等  级
排  名 27824
经  验 545
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校 贵民大
专  业

  自我简介:

TA的其他文章

解题思路:创建链表,合并链表,排序,输出

注意事项:

参考代码:

#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 人评分

  评论区

  • «
  • »