数据结构 线性表 顺序表的归并

举报
悦来客栈的老板 发表于 2020/12/30 01:54:42 2020/12/30
【摘要】 #include <stdio.h>#include <stdlib.h> #define LIST_INIT_SIZE 100#define LISTLNCREMENT 10#define OK 1#define ERROR 0#define OVERFLOW -2 typedef int ElemType; typedef struct{ ElemType *elem; ...

  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LIST_INIT_SIZE 100
  4. #define LISTLNCREMENT 10
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -2
  8. typedef int ElemType;
  9. typedef struct
  10. {
  11. ElemType *elem;
  12. int length;
  13. int listsize;
  14. }SqList;
  15. int InitList_Sq(SqList &L)
  16. {
  17. L.elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));
  18. if (!L.elem)
  19. {
  20. exit(OVERFLOW);
  21. }
  22. L.length = 0;
  23. L.listsize = LIST_INIT_SIZE;
  24. return OK;
  25. }
  26. void Create_Sq(SqList &L, int n)
  27. {
  28. int i;
  29. for (i=0; i<n; i++)
  30. {
  31. scanf("%d",&L.elem[i]);
  32. }
  33. L.length = n;
  34. }
  35. void Display_Sq(SqList L)
  36. {
  37. int i;
  38. for (i=0; i<L.length ; i++)
  39. {
  40. printf("%d ",L.elem[i]);
  41. }
  42. printf("\n");
  43. }
  44. void MergeList_Sq(SqList La, SqList Lb, SqList &Lc)
  45. {/*已知顺序表La和Lb的元素按值非递减排序,归并La和Lb得到新的顺序线性表Lc,Lc的值也是按非递减排序*/
  46. ElemType *pa,*pb,*pc,*pa_last,*pb_last;
  47. pa = La.elem ;
  48. pb = Lb.elem ;
  49. Lc.listsize = Lc.length = La.length + Lb.length ;
  50. pc = Lc.elem = (ElemType *)malloc(Lc.listsize * sizeof(ElemType));
  51. if (!pc)
  52. {
  53. exit(OVERFLOW);
  54. }
  55. pa_last = La.elem + La.length - 1;
  56. pb_last = Lb.elem + Lb.length - 1;
  57. while (pa <= pa_last && pb <= pb_last)
  58. {
  59. if (*pa <= *pb)
  60. {
  61. *pc++ = *pa++;
  62. }
  63. else
  64. {
  65. *pc++ = *pb++;
  66. }
  67. }
  68. while (pa <= pa_last)
  69. {
  70. *pc++ = *pa++;
  71. }
  72. while (pb <= pb_last)
  73. {
  74. *pc++ = *pb++;
  75. }
  76. }
  77. void sort(SqList &L)
  78. {
  79. int i,j;
  80. int temp;
  81. for (i=0; i<L.length-1; i++)
  82. {
  83. for (j=L.length-2; j>=i; j--)
  84. {
  85. if (L.elem[j+1] < L.elem [j])
  86. {
  87. temp = L.elem[j+1] ;
  88. L.elem[j+1] = L.elem[j];
  89. L.elem[j] = temp;
  90. }
  91. }
  92. }
  93. }
  94. int main()
  95. {
  96. int n;
  97. SqList La,Lb,Lc;
  98. InitList_Sq(La);
  99. InitList_Sq(Lb);
  100. printf("请输入链表La的长度: ");
  101. scanf("%d",&n);
  102. printf("请输入 %d 个数据: ",n);
  103. Create_Sq(La,n);
  104. printf("请输入链表Lb的长度: ");
  105. scanf("%d",&n);
  106. printf("请输入 %d 个数据: ",n);
  107. Create_Sq(Lb,n);
  108. sort(La);
  109. sort(Lb);
  110. MergeList_Sq(La,Lb,Lc);
  111. printf("两个链表归并后的链表是: ");
  112. Display_Sq(Lc);
  113. return 0;
  114. }

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/17078337

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。