c++学习笔记—单链表基本操作的实现

时间:2023-03-08 19:59:35

用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表)、结点的查找、删除、排序、打印输出、逆置、链表销毁等基本操作。

IDE:vs2013

c++学习笔记—单链表基本操作的实现

具体实现代码如下:

  1. #include "stdafx.h"
  2. #include <malloc.h>
  3. #include <iostream>
  4. using namespace std;
  5. typedef struct Lnode
  6. {
  7. int data;
  8. struct Lnode *next;
  9. }*node;
  10. node head_creat()   //头插法建立单链表
  11. {
  12. node head = (struct Lnode *)malloc(sizeof(struct Lnode));
  13. head->next = NULL;
  14. node p;
  15. int temp;
  16. while (cin >> temp)
  17. {
  18. p = (node)malloc(sizeof(struct Lnode));
  19. p->data = temp;
  20. p->next = head->next;
  21. head->next=p;
  22. }
  23. return head;
  24. }
  25. bool search(node h, int target)     //查找某元素是否在链表中
  26. {
  27. int flag = 0;
  28. node p = h->next;
  29. if (h->next == NULL)
  30. return false;
  31. for (; p != NULL;)
  32. {
  33. if (p->data == target)
  34. {
  35. flag = 1;
  36. break;
  37. }
  38. else
  39. p++;
  40. }
  41. if (flag)
  42. return true;
  43. else
  44. return false;
  45. }
  46. node back_creat()   //尾插法建立单链表
  47. {
  48. node head = (struct Lnode *)malloc(sizeof(struct Lnode));
  49. head->next = NULL;
  50. node p;
  51. node r = head;
  52. int temp;
  53. while (cin >> temp)
  54. {
  55. p = (node)malloc(sizeof(struct Lnode));
  56. p->data = temp;
  57. r->next=p;
  58. r = p;
  59. }
  60. r->next = NULL;
  61. return head;
  62. }
  63. void print(node h)    //打印单链表
  64. {
  65. node p = h->next;
  66. while (p)
  67. {
  68. cout << p->data << endl;
  69. p = p->next;
  70. }
  71. }
  72. node delete_node(node h,int del_val)     //删除指定值的节点
  73. {
  74. node p = h->next;
  75. node q = h;
  76. node r=NULL;
  77. while (p)
  78. {
  79. if (p->data == del_val)
  80. {
  81. r = p;
  82. p = p->next;
  83. q->next = p;
  84. free(r);
  85. }
  86. else
  87. {
  88. q = p;
  89. p = p->next;
  90. }
  91. }
  92. return h;
  93. }
  94. node sort(node h)     //对链表进行排序(降序)
  95. {
  96. node p=h->next;
  97. if (p->next == NULL)
  98. return h;
  99. for (; p != NULL; p = p->next)
  100. {
  101. for (node q = p->next; q != NULL; q = q->next)
  102. {
  103. int temp;
  104. if (p->data > q->data)
  105. {
  106. temp = p->data;
  107. p->data = q->data;
  108. q->data = temp;
  109. }
  110. }
  111. }
  112. return h;
  113. }
  114. node reverse(node h)  //逆置链表
  115. {
  116. node p, q;
  117. p = h->next;
  118. h->next = NULL;
  119. while (p)
  120. {
  121. q = p;
  122. p = p->next;
  123. q->next = h->next;
  124. h->next = q;
  125. }
  126. return h;
  127. }
  128. void destroy_List(node head)  //销毁链表
  129. {
  130. if (NULL == head)
  131. {
  132. return;
  133. }
  134. if (NULL == head->next)
  135. {
  136. free(head);
  137. head = NULL;
  138. return;
  139. }
  140. node p = head->next;
  141. while (NULL != p)
  142. {
  143. node tmp = p;
  144. p = p->next;
  145. free(tmp);
  146. }
  147. free(head);
  148. head = NULL;
  149. }
  150. int _tmain(int argc, _TCHAR* argv[])
  151. {
  152. cout << "---------------构造链表-------------" << endl;
  153. node h = back_creat();
  154. cout << "---------------打印链表-------------" << endl;
  155. print(h);
  156. //cout << "-----------删除指定值后打印链表-----" << endl;
  157. //h = delete_node(h, 2);
  158. //print(h);
  159. cout << "-----------排序后打印链表------------" << endl;
  160. h = sort(h);
  161. print(h);
  162. cout << "-----------逆置后打印链表------------" << endl;
  163. h = reverse(h);
  164. print(h);
  165. destroy_List(h);
  166. return 0;
  167. }

运行结果:

c++学习笔记—单链表基本操作的实现