当前位置:   article > 正文

C语言实现双链表(带头结点)_请基于输入数据创建一个带头结点的双向链表,并根据位置信息找到对应节点,返回节点

请基于输入数据创建一个带头结点的双向链表,并根据位置信息找到对应节点,返回节点

双链表是在单链表的基础上实现的,不过他们之间又有不同的之处。

1.在使用表头法插入的时候,需要注意的点是要判断表头的下一的元素是否为空(NULL)代码如下:

  1. //4.链表的插入,表头插入法
  2. void insertNewNodebyhead(struct Node* head, int data)
  3. {
  4. struct Node* NewNode = createNewNode(data);//创建节点
  5. NewNode->next = head->next;
  6. if(head->next!=NULL)//如果表头的下一个节点不空的话
  7. head->next->front = NewNode;
  8. NewNode->front = head;
  9. head->next = NewNode;
  10. }

2.在指定位置加入元素时,需要判度该位置是否存在,注意逻辑关系

完整代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5. struct Node* front;
  6. int data;
  7. struct Node* next;
  8. };
  9. //1.创建表头,表示整个链表
  10. struct Node* createlistHead()
  11. {
  12. struct Node* listHead = (struct Node*)malloc(sizeof(struct Node));
  13. //初始化表头
  14. //表头无前驱
  15. //表头的无数据
  16. listHead->next = NULL;
  17. return listHead; //函数为结构体指针函数,即返回结构体变量
  18. }
  19. //2.创建节点为插入做准备
  20. struct Node* createNewNode(int data)
  21. {
  22. struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
  23. NewNode->data = data;
  24. NewNode->next = NULL;
  25. return NewNode;
  26. }
  27. //3.打印链表
  28. void print(struct Node* Head)
  29. {
  30. //创建一个移动指针指向该链表的头结点
  31. struct Node* Pmove;
  32. Pmove = Head->next;//表头为空从下一个节点开始打印
  33. while (Pmove != NULL)
  34. {
  35. printf("%d\t", Pmove->data);
  36. Pmove = Pmove->next;
  37. }
  38. printf("\n");
  39. }
  40. //4.链表的插入,表头插入法
  41. void insertNewNodebyhead(struct Node* head, int data)
  42. {
  43. struct Node* NewNode = createNewNode(data);//创建节点
  44. NewNode->next = head->next;
  45. if(head->next!=NULL)//如果表头的下一个节点不空的话
  46. head->next->front = NewNode;
  47. NewNode->front = head;
  48. head->next = NewNode;
  49. }
  50. //5.表尾插入法
  51. void insertNewNodebytial(struct Node* head, int data)
  52. {
  53. struct Node* NewNode = createNewNode(data);
  54. //创建一个指针去找到表尾
  55. struct Node* Ptail=head;
  56. while (Ptail->next != NULL)
  57. {
  58. Ptail = Ptail->next;
  59. }
  60. //退出循环即找到表尾
  61. NewNode->next = Ptail->next;
  62. NewNode->front = Ptail;
  63. Ptail->next = NewNode;
  64. }
  65. //6.在指定位置前插入
  66. void insertNewNodebyAppoint(struct Node* head, int data,int m)
  67. {
  68. struct Node* NewNode = createNewNode(data);
  69. //创建一个移动指针去找到该位置
  70. struct Node* Pmove = head;
  71. if (head->next == NULL)
  72. {
  73. printf("该链表为空!!!\n");
  74. }
  75. else
  76. {
  77. while (Pmove->data!=m)
  78. {
  79. Pmove = Pmove->next;
  80. if (Pmove == NULL)
  81. {
  82. printf("未能找到该信息!!\n");
  83. return;
  84. }
  85. }
  86. NewNode->next = Pmove;
  87. NewNode->front = Pmove->front;
  88. Pmove->front->next = NewNode;
  89. Pmove->front = NewNode;
  90. }
  91. }
  92. //7.表头删除
  93. void deleteByhead(struct Node* head)
  94. {
  95. if (head->next == NULL)
  96. printf("该链表为空!!!");
  97. else
  98. {
  99. struct Node* Delete = head->next;
  100. head->next = Delete->next;
  101. Delete->next->front = head;
  102. free(Delete);
  103. Delete = NULL;
  104. }
  105. }
  106. //8.表尾删除
  107. void deleteBytial(struct Node* head)
  108. {
  109. struct Node* Deletetial = head->next;
  110. if (Deletetial == NULL)
  111. printf("该链表为空!!!");
  112. else
  113. {
  114. while (Deletetial->next != NULL)
  115. {
  116. Deletetial = Deletetial->next;
  117. }
  118. Deletetial->front->next = Deletetial->next;
  119. free(Deletetial);
  120. Deletetial = NULL;
  121. }
  122. }
  123. //9.删除指定位置的元素
  124. void deleteByAppoint(struct Node* head,int m)
  125. {
  126. //创建一个移动的指针去找到指定位置
  127. struct Node* DeletePmove = head->next;
  128. if (DeletePmove == NULL)
  129. printf("该链表为空!!!!");
  130. else
  131. {
  132. while (DeletePmove->data != m)
  133. {
  134. DeletePmove = DeletePmove->next;
  135. if (DeletePmove == NULL)
  136. {
  137. printf("未能找到该位置!!\n");
  138. return;
  139. }
  140. }
  141. DeletePmove->next->front = DeletePmove->front;
  142. DeletePmove->front->next = DeletePmove->next;
  143. free(DeletePmove);
  144. DeletePmove = NULL;
  145. }
  146. }
  147. int main()
  148. {
  149. int n;
  150. printf("请你输入你要插入数的个数:");
  151. scanf("%d", &n);
  152. printf("表头法插入list1链表:\n");
  153. struct Node* list1 = createlistHead();
  154. for (int i = 0; i < n; i++)
  155. {
  156. int m;
  157. scanf("%d", &m);
  158. insertNewNodebyhead(list1, m);//调用表头法插入函数
  159. }
  160. print(list1);
  161. printf("请输入你要插入的元素个数:");
  162. scanf("%d", &n);
  163. printf("表尾法插入list2链表:\n");
  164. struct Node* list2 = createlistHead();
  165. for (int i = 0; i < n; i++)
  166. {
  167. int m;
  168. scanf("%d", &m);
  169. insertNewNodebytial(list2, m);
  170. }
  171. print(list2);
  172. printf("在指定list2链表中插入元素\n");
  173. int k,l;
  174. printf("请你输入在那个元素之前插入和插入的值:");
  175. scanf("%d %d", &k,&l);
  176. insertNewNodebyAppoint(list2, l, k);
  177. print(list2);
  178. printf("表头删除list2的元素\n");
  179. deleteByhead(list2);
  180. print(list2);
  181. printf("表尾删除list2的元素\n");
  182. deleteBytial(list2);
  183. print(list2);
  184. printf("请你输入删除list2中的元素值:");
  185. int m;
  186. scanf("%d", &m);
  187. deleteByAppoint(list2, m);
  188. print(list2);
  189. system("pause");
  190. return 0;
  191. }

 

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

闽ICP备14008679号