c++有序链表归并

        刚开始写就直接写成了了一个 有序的链表  最后在把两个链表归并是有写了一个冒泡排序来排序  可能有点杂 写的不好见谅 大笑
  1. #pragma once
  2. #include<cstdio>
  3. #include<stdio.h>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<string.h>
  7. #include<string>
  8. #include<utility>
  9. #include<vector>
  10. #include<cstring>
  11. #include<set>
  12. #include <cmath>
  13. #include<math.h>
  14. using namespace std;
  15. struct LIST
  16. {
  17. int data;//数据
  18. LIST *next;//指向下一个位置
  19. LIST(){this->next = NULL;}
  20. };
  21. //数据交换 
  22. inline void change(LIST *p, int data)
  23. {
  24. LIST *temp = (LIST *)malloc(sizeof(LIST));
  25. temp->data = p->data;
  26. temp->next = p->next;
  27. p->data = data;
  28. p->next = temp;
  29. temp = NULL;
  30. free(temp);
  31. return;
  32. }
  33. class  list
  34. {
  35. public :
  36. LIST *head = NULL;//定义链表头
  37. //排序链表用冒泡排序其他的有点难
  38. inline int listsort()
  39. {
  40. LIST *p = head;
  41. while (p != NULL)
  42. {
  43. LIST *temp = p->next;
  44. while (temp!=NULL)
  45. {
  46. //交换两个位置
  47. temp->data = temp->data > p->data ? temp->data : p->data;
  48. p->data = temp->data < p->data ? temp->data : p->data;
  49. temp = temp->next;
  50. }
  51. p = p->next;
  52. }
  53. return 0;
  54.     }
  55. //向链表中添加数据
  56. inline int  insert(int data)
  57. {
  58. if (!head) { head = (LIST *)malloc(sizeof(LIST));head->data = data;head->next = NULL;return 0; }//如果头结点为空就向头结点添加
  59. if (head->data>data)
  60. {
  61. change(head, data);//在头插入
  62. return 0;
  63. }
  64. LIST *p = head->next;
  65. LIST *last = head;
  66. //找到要插入的位子
  67. while (p != NULL)
  68. {
  69. if (p->data > data)
  70. {
  71. change(p, data);//在中间有小的位置就插入
  72. return 0;
  73. }
  74. last = p;
  75. p = p->next;
  76. }
  77. //在尾部插入
  78. p = (LIST *)malloc(sizeof(LIST));
  79. p->data = data;
  80. p->next = NULL;
  81. last->next = p;
  82. last = NULL;
  83. p = NULL;
  84. free(last);
  85. free(p);//释放空间
  86. return 0;
  87. }
  88.    //打印链表
  89. inline int  putout()
  90. {
  91. LIST *p = head;
  92. if (!head) { cout << "该链表为空" << endl; return 0; }
  93. while (p != NULL)
  94. {
  95. cout << p->data << endl;
  96. p = p->next;
  97. }
  98. p = NULL;
  99. free(p);
  100. return 0;
  101. }
  102. //实现归并
  103. inline int  combine(list other)
  104. {
  105. LIST *tail = head;
  106. if (head==NULL) { head = other.head; }
  107. else
  108. {
  109. while (tail->next !=NULL ) { tail = tail->next; }
  110. tail->next = other.head;
  111. }
  112. tail = NULL;
  113. free(tail);
  114. this->listsort();
  115. return 0;
  116. }
  117. };
  118. int main(int agrc, char argv[])
  119. {
  120.  list  head1;
  121.  list  head2;
  122. head2.insert(1);
  123. head2.insert(6);
  124. head2.insert(2);
  125. head2.insert(3);
  126. head2.insert(5);
  127. head1.insert(2);
  128. head1.insert(0);
  129. head1.insert(-1);
  130. head1.insert(1);
  131. cout << "打印连表一:" << endl;
  132. head1.putout();
  133. cout << "打印连表二:" << endl;
  134. head2.putout();
  135. head1.combine(head2);
  136. cout << "打印归并后的连表:" << endl;
  137. head1.putout();
  138. return 0;
  139. }



猜你喜欢

转载自blog.csdn.net/dog_dream/article/details/78820961