• C语言描述数据结构 —— 带头双向循环链表


    1.链表的多种组合

    链表的形态有很多,有不带头单向不循环链表、不带头单向循环链表、不带头双向不循环链表……那么带头或不带头、单向或双向、循环或不循环,能够组合成多种链表。其中最简单的链表是不带头单向不循环链表,最复杂的结构就是带头双向循环链表。当然我们也不要被复杂的结构而吓到,事实上带头双向循环链表的结构复杂,但是实现起来却是非常容易的。

    2.带头双向循环链表的结构

    我们在描述单链表时提到过哨兵卫的概念,这个哨兵卫就是链表的头,也就是说,带头的链表我们不需要单独定义一个指针用来存储头结点的地址,当然传参的时候也不需要使用二级指针了。

    再次强调一点,哨兵卫是不存储任何有效数据的,链表的长度也不行。因为如果链表要存储的数据类型为 char 类型,当链表的长度超过 128 后,那么就会发生错误。

    带头双向循环链表的各个结点有两个指针,分别为 prev 和 next,前者指向上一个结点,后者指向下一个结点。

    3.带头双向循环链表的实现

    3.1链表结点的声明

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

    3.2链表初始化

    1. //链表初始化
    2. ListNode* ListInit()
    3. {
    4. ListNode* guard = (ListNode*)calloc(1, sizeof(ListNode));
    5. assert(guard);
    6. guard->prev = guard;
    7. guard->next = guard;
    8. return guard;
    9. }

    我们使用的带头的链表,所以我们只需要得到哨兵卫的地址就能操作链表。

    3.3尾插和建立新结点

    1. //建立新结点
    2. static ListNode* CreateNode(ListData x)
    3. {
    4. ListNode* node = (ListNode*)calloc(1, sizeof(ListNode));
    5. assert(node);
    6. node->prev = node;
    7. node->next = node;
    8. node->val = x;
    9. return node;
    10. }
    11. //尾插
    12. void ListPushBack(ListNode* phead, ListData x)
    13. {
    14. assert(phead);
    15. ListNode* newnode = CreateNode(x);
    16. ListNode* tail = phead->prev;
    17. phead->prev = newnode;
    18. newnode->next = phead;
    19. newnode->prev = tail;
    20. tail->next = newnode;
    21. }

    尾插的逻辑非常结点,我们不需要像单链表那样去遍历找尾结点或者单独一个存放尾结点地址的指针,可以看到我们的代码找尾结点只有简短的一句代码。事实上我们只需要理清逻辑关系,并且合理改变指针指向即可实现尾插的功能。

    3.4链表的打印

    1. //打印
    2. void ListPrint(ListNode* phead)
    3. {
    4. assert(phead);
    5. ListNode* cur = phead->next;
    6. printf("phead<->");
    7. while (cur != phead)
    8. {
    9. printf("%d<->", cur->val);
    10. cur = cur->next;
    11. }
    12. printf("phead\n");
    13. }

    我们需要注意的是,带头双向循环链表是没有空指针的,那么什么时候停止遍历链表呢?我们需要捋清楚一个逻辑关系,那就是链表的头在哪。  

    3.4头插

    1. //头插
    2. void ListPushHead(ListNode* phead, ListData x)
    3. {
    4. assert(phead);
    5. ListNode* newnode = CreatrNode(x);
    6. //这种写法需要注意链接顺序
    7. /*newnode->next = phead->next;
    8. phead->next->prev = newnode;
    9. phead->next = newnode;
    10. newnode->prev = phead;*/
    11. //这种写法不需要刻意在意链接顺序
    12. ListNode* next = phead->next;
    13. phead->next = newnode;
    14. newnode->prev = phead;
    15. newnode->next = next;
    16. next->prev = newnode;
    17. }

     头插的原理也非常简单,只需要找到链表的头结点即可。然后建立适当的链接关系。

     3.5判空

    1. //判空
    2. bool ListEmpty(ListNode* phead)
    3. {
    4. assert(phead);
    5. return phead->next == phead;
    6. }

    我们一定要注意,链表为空的情况下不是某个结点的next指针指向空,而是next指针指向了哨兵卫。

    3.6尾删

    1. //尾删
    2. void ListPopBack(ListNode* phead)
    3. {
    4. assert(phead);
    5. assert(!ListEmpty(phead));
    6. ListNode* tail = phead->prev;
    7. ListNode* prev = tail->prev;
    8. phead->prev = prev;
    9. prev->next = phead;
    10. free(tail);
    11. }

    尾删的逻辑也非常的简单,我们不需要像单链表那样去遍历链表从而找到尾结点,在这个链表里面,我们仅用两句代码就找到了尾结点和倒数第二个结点,然后从新建立链接关系,即可达到尾删的效果。

    3.7头删

    1. //头删
    2. void ListPopHead(ListNode* phead)
    3. {
    4. assert(phead);
    5. assert(!ListEmpty(phead));
    6. ListNode* cur = phead->next;
    7. ListNode* next = cur->next;
    8. phead->next = next;
    9. next->prev = phead;
    10. free(cur);
    11. }

     3.8查找

    1. //查找
    2. ListNode* ListFind(ListNode* phead, ListData x)
    3. {
    4. assert(phead);
    5. assert(!ListEmpty(phead));
    6. ListNode* cur = phead->next;
    7. while (cur != phead)
    8. {
    9. if (cur->val == x)
    10. return cur;
    11. cur = cur->next;
    12. }
    13. }

    查找的逻辑就非常简单了,我们不需要太多复杂的算法, 只需要遍历一遍链表即可。即使链表的长度有几千或几万,但是对于计算机来说,每秒可以进行几亿次甚至几十亿次的运算,那么效率的损失是微乎其微的。

    3.9在pos位置之前插入

    1. //在pos位置之前插入
    2. void ListInsert(ListNode* pos, ListData x)
    3. {
    4. assert(pos);
    5. ListNode* newnode = CreateNode(x);
    6. ListNode* prev = pos->prev;
    7. prev->next = newnode;
    8. newnode->prev = prev;
    9. newnode->next = pos;
    10. pos->prev = newnode;
    11. }

    在这里有一个问题,如果链表只存在哨兵卫,那么此时还能插入结点吗?当然可以,并且相当于尾插。根据这个原理,我们就可以复用代码,即将头插、尾插的代码做出调整。

    1. //尾插
    2. void ListPushBack(ListNode* phead, ListData x)
    3. {
    4. assert(phead);
    5. //ListNode* newnode = CreateNode(x);
    6. //ListNode* tail = phead->prev;
    7. //phead->prev = newnode;
    8. //newnode->next = phead;
    9. //newnode->prev = tail;
    10. //tail->next = newnode;
    11. ListInsert(phead, x);
    12. }
    1. //头插
    2. void ListPushHead(ListNode* phead, ListData x)
    3. {
    4. assert(phead);
    5. ListNode* newnode = CreateNode(x);
    6. //这种写法需要注意链接顺序
    7. /*newnode->next = phead->next;
    8. phead->next->prev = newnode;
    9. phead->next = newnode;
    10. newnode->prev = phead;*/
    11. //这种写法不需要刻意在意链接顺序
    12. /*ListNode* next = phead->next;
    13. phead->next = newnode;
    14. newnode->prev = phead;
    15. newnode->next = next;
    16. next->prev = newnode;*/
    17. ListInsert(phead->next, x);
    18. }

    3.10删除pos位置的结点

    1. //删除pos位置的结点
    2. void ListErase(ListNode* pos)
    3. {
    4. assert(pos);
    5. assert(!ListEmpty(pos));
    6. ListNode* prev = pos->prev;
    7. ListNode* next = pos->next;
    8. prev->next = next;
    9. next->prev = prev;
    10. free(pos);
    11. }

    这段代码的逻辑关系也非常简单,具体如图:

    与上面一样,这段代码也可以复用,即将头删、尾删的代码做出调整。

    1. //头删
    2. void ListPopHead(ListNode* phead)
    3. {
    4. assert(phead);
    5. assert(!ListEmpty(phead));
    6. //ListNode* cur = phead->next;
    7. //ListNode* next = cur->next;
    8. //phead->next = next;
    9. //next->prev = phead;
    10. //free(cur);
    11. ListErase(phead->next);
    12. }
    1. //尾删
    2. void ListPopBack(ListNode* phead)
    3. {
    4. assert(phead);
    5. assert(!ListEmpty(phead));
    6. /*ListNode* tail = phead->prev;
    7. ListNode* prev = tail->prev;
    8. phead->prev = prev;
    9. prev->next = phead;
    10. free(tail);*/
    11. ListErase(phead->prev);
    12. }

    3.11统计链表的结点个数

    1. //结点个数
    2. size_t ListSize(ListNode* phead)
    3. {
    4. assert(phead);
    5. assert(!ListEmpty(phead));
    6. ListNode* cur = phead->next;
    7. size_t size = 0;
    8. while (cur != phead)
    9. {
    10. size++;
    11. cur = cur->next;
    12. }
    13. return size;
    14. }

    3.12销毁链表

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

    4.完整代码

    4.1 List.h 头文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. #include
    5. #include
    6. typedef int ListData;
    7. typedef struct ListNode
    8. {
    9. ListData val;
    10. struct ListNode* prev;
    11. struct ListNode* next;
    12. }ListNode;
    13. ListNode* ListInit();//链表初始化
    14. void ListPushBack(ListNode* phead, ListData x);//尾插
    15. void ListPushHead(ListNode* phead, ListData x);//头插
    16. void ListPopBack(ListNode* phead);//尾删
    17. void ListPopHead(ListNode* phead);//头删
    18. ListNode* ListFind(ListNode* phead, ListData x);//查找
    19. void ListInsert(ListNode* pos, ListData x);//在pos插入
    20. void ListErase(ListNode* pos);//删除pos位置的结点
    21. size_t ListSize(ListNode* phead);//统计结点个数
    22. bool ListEmpty(ListNode* phead); // 判空
    23. void ListPrint(ListNode* phead);//打印
    24. void ListDestroy(ListNode* phead);

    4.2 List.c 源文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "List.h"
    3. //链表初始化
    4. ListNode* ListInit()
    5. {
    6. ListNode* guard = (ListNode*)calloc(1, sizeof(ListNode));
    7. assert(guard);
    8. guard->prev = guard;
    9. guard->next = guard;
    10. return guard;
    11. }
    12. //建立新结点
    13. static ListNode* CreateNode(ListData x)
    14. {
    15. ListNode* node = (ListNode*)calloc(1, sizeof(ListNode));
    16. assert(node);
    17. node->prev = node;
    18. node->next = node;
    19. node->val = x;
    20. return node;
    21. }
    22. //尾插
    23. void ListPushBack(ListNode* phead, ListData x)
    24. {
    25. assert(phead);
    26. //ListNode* newnode = CreateNode(x);
    27. //ListNode* tail = phead->prev;
    28. //phead->prev = newnode;
    29. //newnode->next = phead;
    30. //newnode->prev = tail;
    31. //tail->next = newnode;
    32. ListInsert(phead, x);
    33. }
    34. //打印
    35. void ListPrint(ListNode* phead)
    36. {
    37. assert(phead);
    38. ListNode* cur = phead->next;
    39. printf("phead<->");
    40. while (cur != phead)
    41. {
    42. printf("%d<->", cur->val);
    43. cur = cur->next;
    44. }
    45. printf("phead\n");
    46. }
    47. //头插
    48. void ListPushHead(ListNode* phead, ListData x)
    49. {
    50. assert(phead);
    51. ListNode* newnode = CreateNode(x);
    52. //这种写法需要注意链接顺序
    53. /*newnode->next = phead->next;
    54. phead->next->prev = newnode;
    55. phead->next = newnode;
    56. newnode->prev = phead;*/
    57. //这种写法不需要刻意在意链接顺序
    58. /*ListNode* next = phead->next;
    59. phead->next = newnode;
    60. newnode->prev = phead;
    61. newnode->next = next;
    62. next->prev = newnode;*/
    63. ListInsert(phead->next, x);
    64. }
    65. //判空
    66. bool ListEmpty(ListNode* phead)
    67. {
    68. assert(phead);
    69. return phead->next == phead;
    70. }
    71. //尾删
    72. void ListPopBack(ListNode* phead)
    73. {
    74. assert(phead);
    75. assert(!ListEmpty(phead));
    76. /*ListNode* tail = phead->prev;
    77. ListNode* prev = tail->prev;
    78. phead->prev = prev;
    79. prev->next = phead;
    80. free(tail);*/
    81. ListErase(phead->prev);
    82. }
    83. //头删
    84. void ListPopHead(ListNode* phead)
    85. {
    86. assert(phead);
    87. assert(!ListEmpty(phead));
    88. //ListNode* cur = phead->next;
    89. //ListNode* next = cur->next;
    90. //phead->next = next;
    91. //next->prev = phead;
    92. //free(cur);
    93. ListErase(phead->next);
    94. }
    95. //查找
    96. ListNode* ListFind(ListNode* phead, ListData x)
    97. {
    98. assert(phead);
    99. assert(!ListEmpty(phead));
    100. ListNode* cur = phead->next;
    101. while (cur != phead)
    102. {
    103. if (cur->val == x)
    104. return cur;
    105. cur = cur->next;
    106. }
    107. }
    108. //在pos位置之前插入
    109. void ListInsert(ListNode* pos, ListData x)
    110. {
    111. assert(pos);
    112. ListNode* newnode = CreateNode(x);
    113. ListNode* prev = pos->prev;
    114. prev->next = newnode;
    115. newnode->prev = prev;
    116. newnode->next = pos;
    117. pos->prev = newnode;
    118. }
    119. //删除pos位置的结点
    120. void ListErase(ListNode* pos)
    121. {
    122. assert(pos);
    123. assert(!ListEmpty(pos));
    124. ListNode* prev = pos->prev;
    125. ListNode* next = pos->next;
    126. prev->next = next;
    127. next->prev = prev;
    128. free(pos);
    129. }
    130. //结点个数
    131. size_t ListSize(ListNode* phead)
    132. {
    133. assert(phead);
    134. assert(!ListEmpty(phead));
    135. ListNode* cur = phead->next;
    136. size_t size = 0;
    137. while (cur != phead)
    138. {
    139. size++;
    140. cur = cur->next;
    141. }
    142. return size;
    143. }
    144. //销毁链表
    145. void ListDestroy(ListNode* phead)
    146. {
    147. assert(phead);
    148. ListNode* cur = phead->next;
    149. while (cur != phead)
    150. {
    151. ListNode* del = cur;
    152. cur = cur->next;
    153. free(del);
    154. }
    155. free(phead);
    156. }

    4.3 test.c 源文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "List.h"
    3. void TestList()
    4. {
    5. ListNode* plist = ListInit();
    6. //头插
    7. ListPushHead(plist, 10);
    8. ListPushHead(plist, 20);
    9. ListPushHead(plist, 30);
    10. //尾插
    11. ListPushBack(plist, 1);
    12. ListPushBack(plist, 2);
    13. ListPushBack(plist, 3);
    14. ListPrint(plist);
    15. //在pos位置之前插入
    16. ListInsert(ListFind(plist, 10), 100);
    17. ListInsert(ListFind(plist, 20), 200);
    18. ListPrint(plist);
    19. printf("%u\n", ListSize(plist));//打印结点个数
    20. //尾删
    21. ListPopBack(plist);
    22. ListPopBack(plist);
    23. //头删
    24. ListPopHead(plist);
    25. ListPopHead(plist);
    26. ListPrint(plist);
    27. //销毁链表
    28. ListDestroy(plist);
    29. }
    30. int main()
    31. {
    32. TestList();
    33. return 0;
    34. }

    4.4运行结果

     

  • 相关阅读:
    [vmware]vmware虚拟机压缩空间清理空间
    python基础项目实战-俄罗斯方块
    object-fit的属性
    java-php-python-ssm计算机office课程平台计算机毕业设计
    【opencv-c++】windows10系统安装OpenCV4.6.0
    图书馆咖啡厅终于开啦,喝了美式和小麦草准备面试的leetcode之旅:82. 删除排序链表中的重复元素 II
    2021年09月 Python(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
    Excel学习 WPS版
    HyperLynx(二)LineSim的基本操作
    JEE.XML配置文件
  • 原文地址:https://blog.csdn.net/weixin_59913110/article/details/126341729