我的解题思路:1,建两个链表(尾插);
2.合二为一;
3.选择排序,边输出,边排序

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Student
  4. {
  5. int number; //存序号
  6. int score; //存成绩
  7. struct Student *next; //链接点
  8. }Student,*L; ////别名*L就是 struct Student * 别名Student就是 struct Student
  9. L newlist(int end); //返回链表的头指针,创建一个链表(尾插)
  10. void connect(L p1,L p2 ); //连接两个链表的函数
  11. void sort(L p1); //排序并输出最后序列。
  12. /***以上为声明****/
  13. int main()
  14. {
  15. int num1,num2;
  16. while(~scanf("%d%d",&num1,&num2))
  17. {
  18. L a=newlist(num1); //创建两个链表
  19. L b=newlist(num2);
  20. connect(a,b); //练接两个链表
  21. sort(a); //排序并输出
  22. }
  23. return 0;
  24. }
  25. L newlist(int end)
  26. {
  27. Student *head; //建立一个头指针,链表的头,不储存数据
  28. head=(Student*)malloc(sizeof(Student)); //开辟空间
  29. if(head==NULL) //判断下还有内存吗
  30. exit(0);
  31. Student *list_end; //建立一个末指针,用于连接,使其next始终=NULL
  32. list_end=head;
  33. int x,i=0,score;
  34. for(i=0;i<end;i++)
  35. {
  36. scanf("%d%d",&x,&score); //输入num1+num2个
  37. Student *p;
  38. p=(Student *)malloc(sizeof(Student)); //一直开辟直到够数
  39. p->number=x;
  40. p->score=score; //存数据
  41. list_end->next=p; //连接在上一个结点上
  42. list_end=p;
  43. }
  44. list_end->next=NULL; //最后指向始终为空
  45. return head;
  46. }
  47. void connect(L p1,L p2 )
  48. {
  49. while(p1->next!=NULL) //扫描第一个链表 ,找到跑p_end
  50. p1=p1->next;
  51. p1->next=p2->next; //把第二个的头连上就好啦
  52. free(p2); //free 掉head的next
  53. }
  54. void sort(L p1)
  55. {
  56. Student *pi,*pj,*temp_p;
  57. for(pi=p1->next;pi!=NULL;pi=pi->next) //选择排序法
  58. {
  59. temp_p=pi; //扫描的第一个number去和后面的number打擂台
  60. for(pj=pi->next;pj!=NULL;pj=pj->next)
  61. if(pj->number<temp_p->number)
  62. temp_p=pj; //记录下最擂主(min)
  63. printf("%d %d\n",temp_p->number,temp_p->score); //输出最小的number和其对应的score;
  64. temp_p->number=pi->number;//因为后续不需要用链表,直接覆盖输出了的数据 (可省些换数据的过程)
  65. temp_p->score=pi->score;
  66. }
  67. }
点赞(1)
 

9.9 分

5 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 2 条评论

林夕 3年前 回复TA
@庞发月 加油
庞发月 3年前 回复TA
注释已经很清楚了,我努力看!