当前位置:   article > 正文

单链表的实现_单链表数据插入: insertnode(snode *head, int pos, int valu

单链表数据插入: insertnode(snode *head, int pos, int value),在单链表第pos(1<
  1. typedef struct node
  2. {
  3. int data;
  4. node *next;
  5. }node;
  6. node* Create(); // 创建单链表
  7. int length(node *head); // 计算单链表的长度
  8. void print(node *head); // 单链表的打印
  9. node* search_node(node* head, int pos); // 单链表节点的查找,从0开始,0返回head节点
  10. bool insert_node(node* head, int pos, int data); // 在第pos个节点之后插入节点
  11. bool delete_node(node* head, int pos); // 删除第pos个节点
  12. void reverse(node* head); // 单链表逆置
  13. node* search_mid(node* head); // 寻找单链表的中间节点
  14. void insert_sort(node* head); // 单链表的正向排序
  15. bool isLoop(node* head, node* start); // 判断单链表是否存在环,start存放环的入口节点
  16. void mergeLink(node* head1, node* head2); // 有序单链表合并
  17. node* Create() // 创建单链表
  18. {
  19. cout << "======================Create()=====================" << endl;
  20. node* head = (node*)malloc(sizeof(node));
  21. assert(head != NULL);
  22. head->data = -1;
  23. bool isFirst = true; // 是否为第一个节点
  24. node* p = head;
  25. node* q = NULL;
  26. int temp;
  27. while (cin >> temp)
  28. {
  29. if (temp == 0)
  30. {
  31. break;
  32. }
  33. p = (node*)malloc(sizeof(node));
  34. assert(p!=NULL);
  35. p->data = temp;
  36. if (isFirst)
  37. {
  38. isFirst = false;
  39. head->next = p;
  40. }
  41. else
  42. {
  43. q->next = p;
  44. }
  45. q = p;
  46. }
  47. p->next = NULL;
  48. return head;
  49. }
  50. int length(node *head) // 计算单链表的长度
  51. {
  52. cout << "==========================length()==========================" << endl;
  53. int count = 0;
  54. node* p = head->next;
  55. while (p != NULL)
  56. {
  57. count++;
  58. p = p->next;
  59. }
  60. return count;
  61. }
  62. void print(node *head) // 单链表的打印
  63. {
  64. cout << "==========================print()==========================" << endl;
  65. node* p = head->next;
  66. while (p != NULL)
  67. {
  68. cout << p->data << " ";
  69. p = p->next;
  70. }
  71. cout << endl;
  72. }
  73. node* search_node(node* head, int pos) // 单链表节点的查找,从0开始,0返回head节点
  74. {
  75. cout << "==========================search_node()==========================" << endl;
  76. if (pos < 0)
  77. {
  78. return NULL;
  79. }
  80. int count = 0;
  81. node* p = head;
  82. while (p != NULL)
  83. {
  84. if (count == pos)
  85. {
  86. return p;
  87. }
  88. count++;
  89. p = p->next;
  90. }
  91. return NULL;
  92. }
  93. bool insert_node(node* head, int pos, int data) // 在第pos个节点之后插入节点
  94. {
  95. cout << "==========================insert_node()==========================" << endl;
  96. if (pos < 0)
  97. {
  98. return false;
  99. }
  100. node* p = search_node(head, pos);
  101. if (p != NULL) // p是尾节点或者中间节点
  102. {
  103. node* q = (node*)malloc(sizeof(node));
  104. assert(q != NULL);
  105. q->data = data;
  106. q->next = p->next;
  107. p->next = q;
  108. return true;
  109. }
  110. return false;
  111. }
  112. bool delete_node(node* head, int pos) // 删除第pos个节点,pos不能为0
  113. {
  114. cout << "==========================delete_node()==========================" << endl;
  115. if (pos <= 0)
  116. {
  117. return false;
  118. }
  119. node* q = search_node(head, pos-1); // 记录第pos个节点的前驱pos-1
  120. node* p = q->next; // 记录第pos个节点
  121. if (p != NULL) // 删除节点P
  122. {
  123. q->next = p->next;
  124. delete p;
  125. p = NULL;
  126. return true;
  127. }
  128. return false;
  129. }
  130. void reverse(node* head) // 单链表逆置
  131. {
  132. cout << "==========================reverse()==========================" << endl;
  133. node* p = head->next; // p用于遍历链表
  134. node* q;
  135. head->next = NULL;
  136. while (p != NULL)
  137. {
  138. q = p; // 记录当前节点
  139. p = p->next;
  140. q->next = head->next; // 将节点p用头插法插入链表
  141. head->next = q;
  142. }
  143. }
  144. node* search_mid(node* head) // 寻找单链表的中间节点
  145. {
  146. cout << "==========================search_mid()==========================" << endl;
  147. int i = 0;
  148. int j = 0;
  149. node* current = head->next;
  150. node* mid = head->next;
  151. while (current != NULL)
  152. {
  153. if (i / 2 > j)
  154. {
  155. j++;
  156. mid = mid->next;
  157. }
  158. i++;
  159. current = current->next;
  160. }
  161. return mid;
  162. }
  163. void insert_sort(node* head) // 单链表的正向排序(升序)
  164. {
  165. cout << "==========================insert_sort()==========================" << endl;
  166. if (head==NULL || head->next == NULL || head->next->next==NULL)
  167. {
  168. return;
  169. }
  170. node* p = head->next->next; // 第二个节点
  171. node* r = NULL;
  172. head->next->next = NULL; // 保留第一个节点,后续的节点使用插入排序
  173. node* pre = head;
  174. node* cur = head->next;
  175. while (p != NULL)
  176. {
  177. pre = head;
  178. cur = head->next;
  179. r = p;
  180. p = p->next;
  181. while (cur != NULL && cur->data < r->data)
  182. {
  183. cur = cur->next;
  184. pre = pre->next;
  185. }
  186. pre->next = r;
  187. r->next = cur;
  188. }
  189. }
  190. bool isLoop(node* head, node* start) // 判断单链表是否存在环,start存放环的入口节点
  191. {
  192. cout << "==========================isLoop()==========================" << endl;
  193. if (head == NULL && head->next == NULL)
  194. {
  195. return false;
  196. }
  197. node* p = head;
  198. node* q = head;
  199. while (q!=NULL && q->next!=NULL && p!=q)
  200. {
  201. p = p->next; // 慢指针
  202. q = q->next->next; // 快指针
  203. }
  204. if (p == q)
  205. {
  206. return true;
  207. }
  208. return false;
  209. }
  210. // 依次将第二个链表中的节点插入到第一个链表中
  211. void mergeLink(node* head1, node* head2) // 有序单链表合并
  212. {
  213. cout << "==========================mergeLink()==========================" << endl;
  214. if (head1 == NULL || head1->next == NULL || head2 == NULL || head2->next == NULL)
  215. {
  216. return;
  217. }
  218. node* p = head2->next; // 第二个链表
  219. node* r = head2;
  220. node* pre = head1; // 第一个链表
  221. node* cur = head1->next;
  222. while (p != NULL)
  223. {
  224. pre = head1;
  225. cur = head1->next;
  226. r = p;
  227. p = p->next;
  228. while (cur != NULL && cur->data < r->data)
  229. {
  230. cur = cur->next;
  231. pre = pre->next;
  232. }
  233. pre->next = r;
  234. r->next = cur;
  235. }
  236. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/902713
推荐阅读
相关标签
  

闽ICP备14008679号