当前位置:   article > 正文

[手撕数据结构] 链表及其基本操作_手撕一个链表数据结构,完成链表的crud。

手撕一个链表数据结构,完成链表的crud。

链表是一种物理存储结构上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。相比于数组这种连续性的储存结构,链表对于插入及删除等操作将更为方便。

单链表

  1. #include<stdlib.h>
  2. #include<stdbool.h>
  3. #include<iostream>
  4. using namespace std;
  5. struct student
  6. {
  7. int score;
  8. struct student* next;
  9. };
  10. typedef struct student stu;
  11. stu* Creat_Node(int x)//申请结点
  12. {
  13. stu* new_node = new stu;
  14. new_node->score = x;
  15. new_node->next = NULL;
  16. return new_node;
  17. }
  18. void Initlist(stu*& L)//初始化 stu* &L表示对一个指针的引用
  19. {
  20. L = new stu;
  21. L->next = NULL;
  22. }
  23. void Insert(int i, int k, stu* L)//在第i个位置插入k;Insert(Get_length(L)+1, 2, L)实现尾插;Insert(1, 2, L);实现头插
  24. {
  25. stu* o = L;
  26. for (int j = 1; j < i; j++)
  27. {
  28. o = o->next;
  29. }
  30. stu* p = new stu;
  31. p->score = k;
  32. p->next = o->next;
  33. o->next = p;
  34. }
  35. void Print_list(stu* head)//遍历打印链表
  36. {
  37. if (head->next == NULL)
  38. {
  39. cout << "空表" << endl;
  40. return;
  41. }
  42. head = head->next;
  43. while (head)
  44. {
  45. cout << head->score << "->";
  46. head = head->next;
  47. }
  48. cout << "NULL" << endl;
  49. return;
  50. }
  51. void Delete_Kth(stu* list, int k)//删除第k个结点
  52. {
  53. for (int i = 1; i < k; i++)
  54. {
  55. list = list->next;
  56. }
  57. stu* p = new stu;
  58. p = list->next;
  59. list->next = list->next->next;
  60. delete p;
  61. return;
  62. }
  63. int Get_length(stu* list)//获得链表长度
  64. {
  65. stu* p = list;
  66. if (p->next == NULL)
  67. {
  68. return 0;
  69. }
  70. int j = 0;
  71. p = p->next;
  72. while (p != NULL)
  73. {
  74. p = p->next;
  75. j++;
  76. }
  77. return j;
  78. }
  79. void Find_first(int k, stu* list)//寻找出现值的第一个结点
  80. {
  81. stu* p = list;
  82. p = p->next;
  83. for (int i = 1; i <= Get_length(list); i++)
  84. {
  85. if (p->score == k)
  86. {
  87. cout << k << "第一次出现的位置为" << i << endl;
  88. return;
  89. }
  90. else
  91. p = p->next;
  92. }
  93. cout << "表中无此值" << endl;
  94. return;
  95. }
  96. void Find_Kth(int k, stu* list)//寻找第K个结点的值
  97. {
  98. stu* p = list;
  99. p = p->next;
  100. int i = 1;
  101. while (p->next != NULL && i < k)//p->next==NULL的时候说明此时p已经是最后一个结点
  102. {
  103. i++;
  104. p = p->next;
  105. }
  106. if (i == k)
  107. {
  108. cout << "第" << k << "个结点的值为" << p->score << endl;
  109. return;
  110. }
  111. else
  112. {
  113. cout << "没有第" << k << "个结点" << endl;
  114. return;
  115. }
  116. }
  117. void Delete_k(int k, stu* list)//删除所有元素为K的结点
  118. {
  119. stu* p = list;
  120. while (p->next)
  121. {
  122. if (p->next->score == k)
  123. {
  124. stu* q = new stu;
  125. q = p->next;
  126. p->next = p->next->next;
  127. delete q;
  128. }
  129. else
  130. p = p->next;
  131. }
  132. return;
  133. }
  134. void sort_list(stu* head)//排序
  135. {
  136. for (int i = 1; i <= Get_length(head); i++)
  137. {
  138. stu* p = head->next;
  139. for (int j = 1; j < Get_length(head); j++)
  140. {
  141. if (p->score > p->next->score)
  142. {
  143. int a = p->score;
  144. p->score = p->next->score;
  145. p->next->score = a;
  146. }
  147. p = p->next;
  148. }
  149. }
  150. }
  151. void Merge_list(stu* L, stu* M)//合并链表于L
  152. {
  153. stu* p = L;
  154. for (int i = 0; i < Get_length(L); i++)
  155. {
  156. p = p->next;
  157. }
  158. p->next = M->next;//表尾下接表头
  159. }
  160. void Remove_DupliCation(stu* head)//除去重复元素
  161. {
  162. stu* p = head->next;
  163. for (int i = 1; i <Get_length(head); i++)
  164. {
  165. if (p->score == p->next->score)
  166. {
  167. stu* q = p->next;
  168. p->next = p->next->next;
  169. delete q;
  170. }
  171. else
  172. p = p->next;
  173. }
  174. }
  175. int main()
  176. {
  177. stu* L;
  178. Initlist(L);
  179. stu* M;
  180. Initlist(M);
  181. Insert(1, 12, L);
  182. Insert(1, 7, L);
  183. Insert(1, 6, L);
  184. Insert(1, 5, L);
  185. cout << "L链表: ";
  186. Print_list(L);
  187. Insert(1, 11, M);
  188. Insert(1, 7, M);
  189. Insert(1, 6, M);
  190. Insert(1, 3, M);
  191. Insert(1, 1, M);
  192. cout << "M链表: ";
  193. Print_list(M);
  194. Merge_list(L, M);
  195. cout << "合并后的链表为:";
  196. Print_list(L);
  197. sort_list(L);
  198. cout << "排序后的链表为:";
  199. Print_list(L);
  200. Remove_DupliCation(L);
  201. cout << "去重后的链表为: ";
  202. Print_list(L);
  203. return 0;
  204. }
  205. //嗨害嗨

双向链表

  1. #include<stdlib.h>
  2. #include<stdbool.h>
  3. #include<iostream>
  4. using namespace std;
  5. #define OK 1;
  6. #define ERROR 0;
  7. #define ElemType int;
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define INFEASIBLE -1
  11. typedef int Status;
  12. struct student
  13. {
  14. int score;
  15. struct student* next;
  16. struct student* last;
  17. };
  18. typedef struct student stu;
  19. Status Initlist(stu* &L)//初始化 stu* &L表示对一个指针的引用
  20. {
  21. L = new stu;
  22. L->next = NULL;
  23. L->last = NULL;
  24. return OK;
  25. }
  26. Status Insert(int i, int k, stu* L)//在第i个位置插入k;Insert(Get_length(L)+1, 2, L)实现尾插;Insert(1, 2, L);实现头插
  27. {
  28. stu* o = L;
  29. for (int j = 1; j < i; j++)
  30. {
  31. o = o->next;
  32. }
  33. stu* p = new stu;
  34. p->score = k;
  35. p->next = o->next;
  36. p->last = o;
  37. o->next = p;
  38. if (p->next != NULL)
  39. {
  40. p ->next->last = p;
  41. }
  42. return OK;
  43. }
  44. Status Print_link(stu* head)//遍历打印链表
  45. {
  46. if (head->next == NULL)
  47. {
  48. cout << "空表" << endl;
  49. return ERROR;
  50. }
  51. head = head->next;
  52. while (head)
  53. {
  54. cout << head->score << endl;
  55. head = head->next;
  56. }
  57. cout << "遍历结束" << endl;
  58. return OK;
  59. }
  60. Status Delete_Kth(stu* list, int k)//删除第k个结点
  61. {
  62. for (int i = 1; i < k; i++)
  63. {
  64. list = list->next;
  65. }
  66. stu* p = new stu;
  67. p = list->next;
  68. list->next = list->next->next;
  69. list->next->last = list;
  70. delete p;
  71. return OK;
  72. }
  73. Status Get_length(stu* list)//获得链表长度
  74. {
  75. stu* p = list;
  76. if (p->next == NULL)
  77. {
  78. return ERROR;
  79. }
  80. int j = 0;
  81. p = p->next;
  82. while (p!=NULL)
  83. {
  84. p = p->next;
  85. j++;
  86. }
  87. return j;
  88. }
  89. Status Find_first(int k, stu* list)//寻找出现值的第一个结点
  90. {
  91. stu* p = list;
  92. p = p->next;
  93. for (int i = 1; i <= Get_length(list); i++)
  94. {
  95. if (p->score == k)
  96. {
  97. cout << k << "第一次出现的位置为" << i << endl;
  98. return OK;
  99. }
  100. else
  101. p = p->next;
  102. }
  103. return ERROR;
  104. }
  105. Status Find_Kth(int k, stu* list)//寻找第K个结点的值
  106. {
  107. stu* p = list;
  108. p = p->next;
  109. int i = 1;
  110. while (p->next!=NULL && i < k)//p->next==NULL的时候说明此时p已经是最后一个结点
  111. {
  112. i++;
  113. p = p->next;
  114. }
  115. if (i == k)
  116. {
  117. cout << "第" << k << "个结点的值为" << p->score << endl;
  118. return OK;
  119. }
  120. else
  121. {
  122. cout << "没有第" << k << "个结点" << endl;
  123. return ERROR;
  124. }
  125. }
  126. Status Delete_k(int k, stu* list)//删除所有元素为K的结点
  127. {
  128. stu* p = list;
  129. while (p->next)
  130. {
  131. if (p->next->score == k)
  132. {
  133. stu* q = new stu;
  134. q = p->next;
  135. p->next = p->next->next;
  136. p->next->last = p;
  137. delete q;
  138. }
  139. else
  140. p = p->next;
  141. }
  142. return OK;
  143. }
  144. Status Clear_link(stu* head)
  145. {
  146. if (head == NULL)
  147. {
  148. return ERROR;
  149. }
  150. stu* p = head;
  151. p = p->next;
  152. while (p != NULL)
  153. {
  154. stu* tmp = p;
  155. p = p->next;
  156. free(tmp);
  157. }
  158. head->next = NULL;
  159. cout << "链表已清空" << endl;
  160. return OK;
  161. }
  162. int main()
  163. {
  164. stu* L;
  165. Initlist(L);
  166. Insert(1, 3, L);
  167. Insert(1, 3, L);
  168. Insert(1, 3, L);
  169. Insert(2, 8, L);
  170. Insert(1, 3, L);
  171. Insert(1, 2, L);
  172. Insert(Get_length(L)+1, 2, L);
  173. Print_link(L);
  174. Delete_k(3, L);
  175. Print_link(L);
  176. return 0;
  177. }
  178. //嗨害嗨

双向循环链表

  1. #include<stdlib.h>
  2. #include<stdbool.h>
  3. #include<iostream>
  4. using namespace std;
  5. struct student
  6. {
  7. int score;
  8. struct student* next;
  9. struct student* last;
  10. };
  11. typedef struct student stu;
  12. void Initlist(stu*& L)//初始化 stu* &L表示对一个指针的引用
  13. {
  14. L = new stu;
  15. L->next = L;
  16. L->last = L;
  17. }
  18. stu* Creat_Node(int x)//申请结点
  19. {
  20. stu* new_node = new stu;
  21. new_node->score = x;
  22. new_node->next = NULL;
  23. new_node->last = NULL;
  24. return new_node;
  25. }
  26. void Print(stu* head)//打印
  27. {
  28. if (head->next == head)
  29. {
  30. cout << "空表" << endl;
  31. return;
  32. }
  33. stu* current = head->next;
  34. while (current != head)
  35. {
  36. cout << current->score << endl;
  37. current = current->next;
  38. }
  39. }
  40. void Tail_Plug(stu* head, int x)//尾插
  41. {
  42. stu* new_node = Creat_Node(x);
  43. new_node->next = head;
  44. new_node->last = head->last;
  45. head->last->next = new_node;
  46. head->last = new_node;
  47. }
  48. void Delete_tail(stu* head)//尾删
  49. {
  50. stu* p = head->last;
  51. p->last->next = head;
  52. head->last = p->last;
  53. delete p;
  54. }
  55. void Head_Plug(stu* head, int x)//头插
  56. {
  57. stu* p = Creat_Node(x);
  58. p->next = head->next;
  59. head->next->last = p;
  60. p->last = head;
  61. head->next = p;
  62. }
  63. void Delete_head(stu* head)//头删
  64. {
  65. stu* p = head->next;
  66. head->next = p->next;
  67. p->next->last = head;
  68. delete p;
  69. }
  70. int Get_length(stu* head)//获得链表长度
  71. {
  72. stu* p = head;
  73. if (p->next == NULL)
  74. {
  75. return 0;
  76. }
  77. int j = 0;
  78. while (p->next != head)
  79. {
  80. p = p->next;
  81. j++;
  82. }
  83. return j;
  84. }
  85. void Insert(int i, int k, stu* L)//在第i个位置插入k;
  86. {
  87. if (i <= Get_length(L))
  88. {
  89. stu* o = L;
  90. for (int j = 1; j < i; j++)
  91. {
  92. o = o->next;
  93. }
  94. stu* p = Creat_Node(k);
  95. p->next = o->next;
  96. p->last = o;
  97. o->next = p;
  98. }
  99. else
  100. cout << "第" << i << "个位置不合法" << endl;
  101. }
  102. void Delete_Kth(stu* list, int k)//删除第k个结点
  103. {
  104. if (k > Get_length(list) || k < 1)
  105. {
  106. cout << "没有第" << k << "个结点" << endl;
  107. return;
  108. }
  109. for (int i = 1; i < k; i++)
  110. {
  111. list = list->next;
  112. }
  113. stu* p = list->next;
  114. list->next = list->next->next;
  115. list->next->last = list;
  116. delete p;
  117. }
  118. void Find_first(int k, stu* list)//寻找出现值的第一个结点
  119. {
  120. stu* p = list;
  121. p = p->next;
  122. for (int i = 1; i <= Get_length(list); i++)
  123. {
  124. if (p->score == k)
  125. {
  126. cout << k << "第一次出现的位置为" << i << endl;
  127. return;
  128. }
  129. else
  130. p = p->next;
  131. }
  132. cout << "表中没有值为" << k << "的结点" << endl;
  133. }
  134. void Find_Kth(int k, stu* head)//寻找第K个结点的值
  135. {
  136. int i = k;
  137. if (k <= Get_length(head))
  138. {
  139. stu* p = head;
  140. while (i--)
  141. {
  142. p = p->next;
  143. }
  144. cout << "第" << k << "个结点的值为" << p->score << endl;
  145. return;
  146. }
  147. else
  148. {
  149. cout << "没有第" << k << "个结点" << endl;
  150. return;
  151. }
  152. }
  153. void Delete_k(int k, stu* list)//删除所有元素为K的结点
  154. {
  155. stu* p = list;
  156. while (p->next != list)
  157. {
  158. if (p->next->score == k)
  159. {
  160. stu* q = new stu;
  161. q = p->next;
  162. p->next = p->next->next;
  163. p->next->last = p;
  164. delete q;
  165. }
  166. else
  167. p = p->next;
  168. }
  169. }
  170. void Clear_link(stu* head)//清空链表
  171. {
  172. int k = Get_length(head);
  173. for (int i = 1; i <= k; i++)
  174. {
  175. Delete_Kth(head, 1);
  176. }
  177. cout << "链表已清空" << endl;
  178. }
  179. int main()
  180. {
  181. stu* L;
  182. Initlist(L);
  183. Tail_Plug(L, 1);
  184. Tail_Plug(L, 2);
  185. Tail_Plug(L, 3);
  186. Tail_Plug(L, 4);
  187. Head_Plug(L, 8);
  188. Insert(2, 8, L);
  189. Insert(2, 8, L);
  190. Insert(2, 8, L);
  191. Insert(2, 8, L);
  192. Insert(108, 5, L);
  193. Print(L);
  194. Clear_link(L);
  195. Print(L);
  196. return 0;
  197. }
  198. //嗨害嗨

有何不足欢迎批评指正

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

闽ICP备14008679号