当前位置:   article > 正文

数据结构——带头结点双向循环链表_双向循环链表的头指针为head,若带头结点,则表空的条件

双向循环链表的头指针为head,若带头结点,则表空的条件

相比较与单链表,双向循环链表每个结点多了一个prev指针域,用于指向该结点的前驱,并且链表的头尾结点也用指针域相连。所以对于带头结点的双向循环链表的判空条件为head->next=head;除此之外,双向循环链表的基本操作自然也与单链表有较大区别,接下来就让我们来逐步实现。

1.结构体定义

  1. typedef int LTDataType;
  2. typedef struct ListNode
  3. {
  4. LTDataType data;
  5. struct ListNode* next;
  6. struct ListNode* prev;
  7. }ListNode;

data为结点数据域,next指针指向当前结点的后继,prev指针指向当前结点的前驱。

2.基本操作实现

  1. //测试函数
  2. void Test_list();
  3. //双链表判空
  4. int ListEmpty(ListNode* pHead);
  5. //动态申请一个结点data
  6. ListNode* buyDListNode(LTDataType data);
  7. // 创建返回链表的头结点.
  8. ListNode* ListCreate();
  9. // 双向链表销毁
  10. void ListDestory(ListNode* pHead);
  11. // 双向链表打印
  12. void ListPrint(ListNode* pHead);
  13. // 双向链表尾插
  14. void ListPushBack(ListNode* pHead, LTDataType x);
  15. // 双向链表尾删
  16. void ListPopBack(ListNode* pHead);
  17. // 双向链表头插
  18. void ListPushFront(ListNode* pHead, LTDataType x);
  19. // 双向链表头删
  20. void ListPopFront(ListNode* pHead);
  21. // 双向链表查找
  22. ListNode* ListFind(ListNode* pHead, LTDataType x);
  23. // 双向链表在pos的前面进行插入
  24. void ListInsert(ListNode* pos, LTDataType x);
  25. // 双向链表删除pos位置的节点
  26. void ListErase(ListNode* pos);

2.1动态申请结点

动态申请结点实现是后续实现链表的头结点创建和新结点插入的子功能,提前用函数封装好方便于后续操作函数的实现。具体实现:(1)通过malloc在堆上动态申请空间。(2)对新结点数据域对应赋值。(3)将结点前后指针域置空。

代码实现:

  1. ListNode* buyDListNode(LTDataType data) {//动态申请结点
  2. ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
  3. if (newhead == NULL) {//申请失败
  4. assert(0);
  5. return NULL;
  6. }
  7. newhead->data = data;
  8. newhead->next = NULL;
  9. newhead->prev = NULL;
  10. return newhead;
  11. }

2.2创建并返回链表头结点

通过动态申请结点函数创建头结点,头结点数据域初始化为0(或-1),初始状态下链表没有其余结点,所有将头结点的前后指针域都指向头结点本身。

代码实现:

  1. // 创建返回链表的头结点.
  2. ListNode* ListCreate() {
  3. ListNode* pHead = buyDListNode(0);
  4. pHead->next = pHead;
  5. pHead->prev = pHead;
  6. return pHead;
  7. }

2.3链表打印

通过简单循环便可实现:

  1. // 双向链表打印
  2. void ListPrint(ListNode* pHead) {
  3. assert(pHead);
  4. ListNode* cur = pHead->next;
  5. while (cur != pHead) {
  6. printf("%d ", cur->data);
  7. cur = cur->next;
  8. }
  9. printf("\n");
  10. }

2.4链表尾插

对于双向链表的插入,我们只需注意要先将结点连接进链表再断开插入位置前后结点的联系即可,

实现也是比较简单,可以通过画图辅助,过程更加清晰:

  1. // 双向链表尾插
  2. void ListPushBack(ListNode* pHead, LTDataType x) {
  3. ListNode* node = buyDListNode(x);
  4. //先不断开原链表,将新结点连接进去
  5. node->prev = pHead->prev;
  6. node->next = pHead;
  7. node->prev->next = node;
  8. pHead->prev = node;
  9. }

2.5链表头插

头插需注意的是是将结点插入在头结点之后,原理和尾插相同。

  1. void ListPushFront(ListNode* pHead, LTDataType x) {
  2. assert(pHead);
  3. ListNode* node = buyDListNode(x);
  4. //先不断开原链表,将新结点连接进去
  5. node->next = pHead->next;
  6. node->prev = pHead;
  7. pHead->next = node;
  8. node->next->prev = node;
  9. }

2.6链表任意位置插入

  1. // 双向链表在pos的前面进行插入
  2. void ListInsert(ListNode* pos, LTDataType x) {
  3. assert(pos);
  4. ListNode* node = buyDListNode(x);
  5. //先不断开原链表,将新结点连接进去
  6. node->next = pos;
  7. node->prev = pos->prev;
  8. node->prev->next = node;
  9. pos->prev = node;
  10. }

2.7链表删除

三种删除操作其操作也是相同的,只需理清结点指针域关系即可,通过画图即可清晰理清逻辑关系,所以这里直接给上代码:

  1. // 双向链表尾删
  2. void ListPopBack(ListNode* pHead) {
  3. assert(pHead);
  4. ListNode* delNode = NULL;
  5. if (ListEmpty(pHead)) {
  6. return;
  7. }
  8. delNode = pHead->prev;
  9. delNode->prev->next = pHead;
  10. pHead->prev = delNode->prev;
  11. free(delNode);
  12. }
  13. // 双向链表头删
  14. void ListPopFront(ListNode* pHead) {
  15. assert(pHead);
  16. if (ListEmpty(pHead)) {
  17. return;
  18. }
  19. ListNode* cur = pHead->next;
  20. pHead->next = pHead->next->next;
  21. pHead->next->prev = pHead;
  22. free(cur);
  23. }
  24. // 双向链表删除pos位置的节点
  25. void ListErase(ListNode* pos) {
  26. assert(pos);
  27. if (pos == NULL) {
  28. return;
  29. }
  30. ListNode* cur = pos;
  31. cur->prev->next = cur->next;
  32. cur->next->prev = cur->prev;
  33. free(cur);
  34. }

2.8链表查找

查找与打印主要都是遍历链表的过程,需注意的是循环条件不再是临时结点循环到NULL,而是结点遍历返回到头结点,即cur!=head:

  1. // 双向链表查找
  2. ListNode* ListFind(ListNode* pHead, LTDataType x) {
  3. assert(pHead);
  4. if (ListEmpty(pHead)) {
  5. return NULL;
  6. }
  7. ListNode* cur = pHead->next;
  8. while (cur != pHead) {
  9. if (cur->data == x) {
  10. return cur;
  11. }
  12. cur = cur->next;
  13. }
  14. return NULL;
  15. }

2.9链表销毁

链表销毁只需遍历链表逐步释放结点即可,在释放结点之前需临时保存下个结点,以免后续结点丢失:

  1. // 双向链表销毁
  2. void ListDestory(ListNode* pHead) {
  3. assert(pHead);
  4. ListNode* head = pHead;
  5. ListNode* cur = head->next;
  6. while (cur != head) {
  7. ListNode* next = cur->next;
  8. free(cur);
  9. cur = next;
  10. }
  11. free(head);
  12. head = NULL;
  13. }

3.功能测试

  1. void Test_list() {
  2. ListNode* list = ListCreate();
  3. ListPushBack(list, 2);
  4. ListPushBack(list, 3);
  5. ListPushBack(list, 4);
  6. ListPushBack(list, 5);
  7. ListPushFront(list, 1);
  8. ListPushFront(list, 0);
  9. ListPrint(list);
  10. ListPopBack(list);
  11. ListPopBack(list);
  12. ListPopFront(list);
  13. ListPrint(list);
  14. ListInsert(ListFind(list, 3), 99);
  15. ListPrint(list);
  16. ListErase(ListFind(list, 3));
  17. ListPrint(list);
  18. ListDestory(list);
  19. }

测试结果:

4.完整代码

  1. #include<stdio.h>
  2. #include<assert.h>
  3. #include<malloc.h>
  4. // 带头+双向+循环链表增删查改实现
  5. typedef int LTDataType;
  6. typedef struct ListNode
  7. {
  8. LTDataType data;
  9. struct ListNode* next;
  10. struct ListNode* prev;
  11. }ListNode;
  12. //测试函数
  13. void Test_list();
  14. //双链表判空
  15. int ListEmpty(ListNode* pHead);
  16. //动态申请一个结点data
  17. ListNode* buyDListNode(LTDataType data);
  18. // 创建返回链表的头结点.
  19. ListNode* ListCreate();
  20. // 双向链表销毁
  21. void ListDestory(ListNode* pHead);
  22. // 双向链表打印
  23. void ListPrint(ListNode* pHead);
  24. // 双向链表尾插
  25. void ListPushBack(ListNode* pHead, LTDataType x);
  26. // 双向链表尾删
  27. void ListPopBack(ListNode* pHead);
  28. // 双向链表头插
  29. void ListPushFront(ListNode* pHead, LTDataType x);
  30. // 双向链表头删
  31. void ListPopFront(ListNode* pHead);
  32. // 双向链表查找
  33. ListNode* ListFind(ListNode* pHead, LTDataType x);
  34. // 双向链表在pos的前面进行插入
  35. void ListInsert(ListNode* pos, LTDataType x);
  36. // 双向链表删除pos位置的节点
  37. void ListErase(ListNode* pos);
  38. int main() {
  39. Test_list();
  40. return 0;
  41. }
  42. void Test_list() {
  43. ListNode* list = ListCreate();
  44. ListPushBack(list, 2);
  45. ListPushBack(list, 3);
  46. ListPushBack(list, 4);
  47. ListPushBack(list, 5);
  48. ListPushFront(list, 1);
  49. ListPushFront(list, 0);
  50. ListPrint(list);
  51. ListPopBack(list);
  52. ListPopBack(list);
  53. ListPopFront(list);
  54. ListPrint(list);
  55. ListInsert(ListFind(list, 3), 99);
  56. ListPrint(list);
  57. ListErase(ListFind(list, 3));
  58. ListPrint(list);
  59. ListDestory(list);
  60. }
  61. ListNode* buyDListNode(LTDataType data) {//动态申请结点
  62. ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
  63. if (newhead == NULL) {
  64. assert(0);
  65. return NULL;
  66. }
  67. newhead->data = data;
  68. newhead->next = NULL;
  69. newhead->prev = NULL;
  70. return newhead;
  71. }
  72. // 创建返回链表的头结点.
  73. ListNode* ListCreate() {
  74. ListNode* pHead = buyDListNode(0);
  75. pHead->next = pHead;
  76. pHead->prev = pHead;
  77. return pHead;
  78. }
  79. // 双向链表销毁
  80. void ListDestory(ListNode* pHead) {
  81. assert(pHead);
  82. ListNode* head = pHead;
  83. ListNode* cur = head->next;
  84. while (cur != head) {
  85. ListNode* next = cur->next;
  86. free(cur);
  87. cur = next;
  88. }
  89. free(head);
  90. head = NULL;
  91. }
  92. // 双向链表打印
  93. void ListPrint(ListNode* pHead) {
  94. assert(pHead);
  95. ListNode* cur = pHead->next;
  96. while (cur != pHead) {
  97. printf("%d->", cur->data);
  98. cur = cur->next;
  99. }
  100. printf("NULL\n");
  101. }
  102. // 双向链表尾插
  103. void ListPushBack(ListNode* pHead, LTDataType x) {
  104. ListNode* node = buyDListNode(x);
  105. //先不断开原链表,将新结点连接进去
  106. node->prev = pHead->prev;
  107. node->next = pHead;
  108. node->prev->next = node;
  109. pHead->prev = node;
  110. }
  111. // 双向链表判空
  112. int ListEmpty(ListNode* pHead) {//判空,空返回1,非空返回0
  113. assert(pHead);
  114. return pHead->next == pHead;
  115. }
  116. // 双向链表尾删
  117. void ListPopBack(ListNode* pHead) {
  118. assert(pHead);
  119. ListNode* delNode = NULL;
  120. if (ListEmpty(pHead)) {
  121. return;
  122. }
  123. delNode = pHead->prev;
  124. delNode->prev->next = pHead;
  125. pHead->prev = delNode->prev;
  126. free(delNode);
  127. }
  128. // 双向链表头插
  129. void ListPushFront(ListNode* pHead, LTDataType x) {
  130. assert(pHead);
  131. ListNode* node = buyDListNode(x);
  132. //先不断开原链表,将新结点连接进去
  133. node->next = pHead->next;
  134. node->prev = pHead;
  135. pHead->next = node;
  136. node->next->prev = node;
  137. }
  138. // 双向链表头删
  139. void ListPopFront(ListNode* pHead) {
  140. assert(pHead);
  141. if (ListEmpty(pHead)) {
  142. return;
  143. }
  144. ListNode* cur = pHead->next;
  145. pHead->next = pHead->next->next;
  146. pHead->next->prev = pHead;
  147. free(cur);
  148. }
  149. // 双向链表查找
  150. ListNode* ListFind(ListNode* pHead, LTDataType x) {
  151. assert(pHead);
  152. if (ListEmpty(pHead)) {
  153. return NULL;
  154. }
  155. ListNode* cur = pHead->next;
  156. while (cur != pHead) {
  157. if (cur->data == x) {
  158. return cur;
  159. }
  160. cur = cur->next;
  161. }
  162. return NULL;
  163. }
  164. // 双向链表在pos的前面进行插入
  165. void ListInsert(ListNode* pos, LTDataType x) {
  166. assert(pos);
  167. ListNode* node = buyDListNode(x);
  168. //先不断开原链表,将新结点连接进去
  169. node->next = pos;
  170. node->prev = pos->prev;
  171. node->prev->next = node;
  172. pos->prev = node;
  173. }
  174. // 双向链表删除pos位置的节点
  175. void ListErase(ListNode* pos) {
  176. assert(pos);
  177. if (pos == NULL) {
  178. return;
  179. }
  180. ListNode* cur = pos;
  181. cur->prev->next = cur->next;
  182. cur->next->prev = cur->prev;
  183. free(cur);
  184. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号