• 双链表实现,增 删 改 查(基础详细版)


    0.在开始之前建议先跟着思路,走一遍,调试部分我就不放了主要写的是实现思路。当然最后也会把源码附上。

    1. 带头双向循环链表(简称:双向链表)

    1. 双向循环带头链表:
      1. 红色的指向正的 最后一个节点指向头结点
      2. 绿色的指向反的 从最后一个开始遍历,直到头结点
      3. 头结点里存储着指向尾节点的和头结点的
      4. 比如:phead->next = pcur ; phead->prev = ptail;
    2. 每个节点的当前节点都保存着指向下一个节点的地址指向上一个节点的地址
    3. 代码的基本结构
    1. typedef int LTDataType;
    2. typedef struct ListNode
    3. {
    4. struct ListNode* next; //指针保存下⼀个节点的地址
    5. struct ListNode* prev; //指针保存前⼀个节点的地
    6. LTDataType data;
    7. }LTNode;
    1. 单链表和双链表为NULL时的两种不一样的情况
      1. 单链表:第一个直接置为NULL
      2. 双链表: 只有一个带头链表,里面不存储任何有效的数据,双向链表不能置为NULL,要让他指向自己实现自循环

    1.1. 单链表和双链表与指针的关系

    1. 双链表的本质,就是不会对头节点改变,而单链表头插对头节点改变了,所以要二级指针
    2. 区别就是要不要对(单链表和双链表)的头指针改变
    3. 举个例子:对于单链表的头指针,有些地方也可以不用传二级指针

            就比如尾插时不用对头指针改变,而头插就需要对头指针改变了

    1. 已知传过去的是创建好有空间的节点,此时尾插不需要二级指针
    2. 总结概括:改变单链表头指针指向就要传二级地址。双链表不要改变头节点

    2. 代码实现

    1. 在双链表中不用改变头结点,要改变的是结构体内前后指针的指向。和改变头结点的指向完全是两个概念
    2. 在实现也是要分三个文件的,DouList.c  DouList.h  test.c 。功能的实现,函数和类型的声明,测试文件
    3. 这里讲的是核心部分,也就是功能的实现

    2.1. 双链表基本结构

    1. prev指向上一个节点,next指向下一个节点
    1. typedef int DLDataType;
    2. typedef struct ListNode
    3. {
    4. DLDataType data;
    5. struct ListNode* prev;//指向上一个节点
    6. struct ListNode* next;//指向下一个节点
    7. }DLNode;//链表节点

    2.1.1. 初始化

    1. //第二种初始化方法为了保证接口的一致性,让传递的值都是一级指针
    2. DLNode* DLInit()
    3. {
    4. DLNode* node = DLBuyNode(-1);
    5. return node;
    6. }
    7. DLNode* DLInit()
    8. {
    9. DLNode* node = DLBuyNode(-1);
    10. return node;
    11. }
    12. //test.c 通过接收返回值的方式
    13. void DListTest1()
    14. {
    15. DLNode* plist = NULL;//哨兵位
    16. plist = DLInit();
    17. }

    2.1.2. 空间申请

    1. 空间申请部分很简单,就是创建新的节点,然后赋值上需要插入的值
    2. 接着让新创建的节点实现自循环,因为这个是双链表
    1. //新空间
    2. DLNode* DLBuyNode(DLDataType x)
    3. {
    4. DLNode* newnode = (DLNode*)malloc(sizeof(DLNode));
    5. if (newnode == NULL)
    6. {
    7. perror("malloc nwe");
    8. exit(-1);//推出整个程序
    9. }
    10. newnode->data = x;
    11. newnode->next = newnode->prev = newnode;//申请新节点,需要自循环
    12. return newnode;
    13. }

    2.2. 尾插

    1. 不管是头插还是尾插都不需要改变头节点的位置
    2. 头节点既不能删除也不能改变,没必要传二级指针
    3. 头节点不能为NULL,因为这是双链表
    4. phead->prev->next = newnode;必须写在前面,不然会找不到尾节点
    1. void DLPushBack(DLNode* phead, DLDataType x)
    2. {
    3. assert(phead);
    4. DLNode* newnode = DLBuyNode(x);
    5. //phead phead->prev newnode
    6. newnode->prev = phead->prev;//phead->prev 是头节点指向的上一个节点也就是尾节点
    7. newnode->next = phead;
    8. phead->prev->next = newnode;//尾节点指向的下一个节点
    9. phead->prev = newnode;
    10. }

    2.2.1. 空间申请

    1. 空间申请部分很简单,就是创建新的节点,然后赋值上需要插入的值
    2. 接着让新创建的节点实现自循环,因为这个是双链表
    1. //新空间
    2. DLNode* DLBuyNode(DLDataType x)
    3. {
    4. DLNode* newnode = (DLNode*)malloc(sizeof(DLNode));
    5. if (newnode == NULL)
    6. {
    7. perror("malloc nwe");
    8. exit(-1);//推出整个程序
    9. }
    10. newnode->data = x;
    11. newnode->next = newnode->prev = newnode;//申请新节点,需要自循环
    12. return newnode;
    13. }
    1. 这张图是什么意思呢?意思是在头节点左边插入其实也是尾插,你可以想象一下,把链表想成环形

    2.3. 打印链表

    1. 打印链表需要有两个注意的点,这个不同于单链表,结束条件不能为pcur != NULL;也不可以是pcur->next != NULL,打印到尾节点时会停下循环不进尾节点
    2. 而是pcur != phead; 头节点不存储有效数据,也就不用打印,拿这个作为结束条件再好不过
    1. void DLPrint(DLNode* phead)
    2. {
    3. DLNode* pcur = phead->next;//头节点指向下一个节点,也就是第一个有效节点
    4. while (pcur != phead)
    5. {
    6. printf("%d->", pcur->data);
    7. pcur = pcur->next;//遍历指向下一个节点
    8. }
    9. printf("\n");
    10. }

    2.4. 头插

    1. 尾插:在头节点的前面尾插,看上面的图newnode的位置意思是一样的
    2. 头插:在头节点的后面头插
    3. 方法的先后顺序是一定不能改变的
    1. void DLPushFront(DLNode* phead,DLDataType x)
    2. {
    3. assert(phead);//头节点不能为NULL
    4. DLNode* newnode = DLBuyNode(x);
    5. newnode->next = phead->next;
    6. newnode->prev = phead;
    7. //方法1
    8. //phead->next->prev = newnode;//第一个有效节点指向newnode
    9. //phead->next = newnode;//指向下一个节点
    10. //方法2
    11. phead->next = newnode;
    12. newnode->next->prev = newnode;
    13. }
    1. 注意:要搞清楚头插和尾插时候的前后顺序

    2.5. 尾删

    1. 如果不保存del节点会导致内存泄露
    2. 尾删和头删可以发现,不用在乎谁先指向谁
    3. 只要不满足phead为非NULL 和 phead->next != phead就会报错
    1. void DLDelBack(DLNode* phead)
    2. {
    3. //当phead->next != phead就正常执行
    4. assert(phead && phead->next != phead);//链表都为NULL你删什么?,如果是只剩一个头结点当然也不行
    5. DLNode* del = phead->prev;
    6. del->prev->next = phead;//删除的前一个节点指向phead
    7. phead->prev = del->prev;//头结点的前一个节点指向del->prev
    8. free(del);
    9. del = NULL;
    10. }

    2.6. 头删

    1. 头删很简单,就是先保存del节点
    2. 让phead->next指向del->next ,然后让del->next->prev= phead;
    1. void DLDelFront(DLNode* phead)
    2. {
    3. assert(phead && phead->next != phead);//如果保错就说明,要么为NULL,要么只有一个头结点
    4. DLNode* del = phead->next;//第一个有效节点
    5. phead->next = del->next;
    6. del->next->prev = phead;
    7. free(del);
    8. del = NULL;
    9. }

    2.7. 查找

    1. 查找很简单啦,这就不讲了,唯一和单链表不同的是结束条件,pcur != phead,当遍历到头节点的时候就结束
    1. //查找
    2. DLNode* DLFind(DLNode* phead, DLDataType x)
    3. {
    4. assert(phead);
    5. DLNode* pcur = phead->next;//可能存在多次查找,其次记得是第一个有效节点
    6. while (pcur != phead)
    7. {
    8. if (pcur->data == x)
    9. {
    10. return pcur;
    11. }
    12. pcur = pcur->next;
    13. }
    14. return NULL;
    15. }
    16. //test.c部分
    17. DLNode* find = DLFind(plist, 22);
    18. if (find == NULL)
    19. printf("没找到");
    20. else
    21. printf("找到了");

    2.8. 在指定位置之前插入

    1. 两种情况

    1. //在指定位置插入
    2. void DLInsert(DLNode* pos, DLDataType x)
    3. {
    4. assert(pos);//防止phead == NULL链表
    5. DLNode* newnode = DLBuyNode(x);
    6. //pos newnode pos->next
    7. newnode->next = pos->next;
    8. newnode->prev = pos;
    9. // 2,3点
    10. pos->next->prev = newnode;
    11. pos->next = newnode;
    12. }

    2.9. pos位置删除 

    1. pos位置删除,pos左右两边的节点手拉手就行

    1. //在指定位置删除
    2. void DLErase(DLNode* pos)
    3. {
    4. assert(pos);//可能会缺少phead的检查
    5. //pos->prev pos pos->next
    6. pos->next->prev = pos->prev;
    7. pos->prev->next = pos->next;
    8. free(pos);//pos置为NULL并不影响find
    9. pos = NULL;
    10. }

    3. 初始化的方式

    1. 为了保证接口的一致性
    2. 假如说这些方法,这个方法要传一级指针,那个方法要穿二级指针,总是不能统一,在后续开发中不会大大增加你的开发难度吗?
    1. //新空间
    2. DLNode* DLBuyNode(DLDataType x)
    3. {
    4. DLNode* newnode = (DLNode*)malloc(sizeof(DLNode));
    5. if (newnode == NULL)
    6. {
    7. perror("malloc nwe");
    8. exit(-1);//推出整个程序
    9. }
    10. newnode->data = x;
    11. newnode->next = newnode->prev = newnode;//申请新节点,需要自循环
    12. return newnode;
    13. }
    14. //第一种,双链表的初始化
    15. //void DLInit(DLNode** pphead)//只传参不返回值,因为可以对实参直接改变
    16. //{
    17. // *pphead = DLBuyNode(-1);
    18. //}
    19. //第二种初始化方法为了保证接口的一致性,让传递的值都是一级指针
    20. DLNode* DLInit()
    21. {
    22. DLNode* node = DLBuyNode(-1);
    23. return node;
    24. }
    1. void DListTest1()
    2. {
    3. //第一种
    4. //DLNode* plist = NULL;//哨兵位
    5. //DLInit(&plist);//对头节点改变需要二级指针
    6. //第二种初始化方式
    7. DLNode* plist = DLInit();
    8. }

    4. 链表的销毁

    1. 形参是一级指针,传参的时候也是一级指针,这是传值调用了。但是free这个函数只管释放掉动态开辟的空间
    2. 假如用第一点的方式free掉头节点,并置为NULL,会导致实参这边指针变量没有置为NULL,为什么呢?
    3. 因为形参的改变没有影响实参(这个传的是一级指针),而有了两个变量名,虽然空间释放了,但是实参没受影响,所以也要置为NULL。
    4. 为什么不传二级指针直接改变实参头指针的指向呢,因为要保持接口的一致性
    1. //销毁链表
    2. void DLDesTroy(DLNode* phead)
    3. {
    4. assert(phead);//此时链表可以为NULL
    5. DLNode* pcur = phead->next;
    6. while (pcur != phead)
    7. {
    8. DLNode* node = pcur->next;//保存后一个节点
    9. free(pcur);
    10. pcur = node;//把后一个节点给到pcur
    11. }
    12. free(phead);
    13. phead = NULL;//两个名字都有访问这块空间的权限,但是这个函数里的名字,并不会影响传过来之前的变量名
    14. }
    1. free,原本意思对动态开辟的内存进行释放,函数内和实参这边的函数都有访问权限
    2. 可以理解为,两个地方都可以对这个块空间释放,但是有两个变量名所以,实参函数这边的指针就变成了野指针
    3. 而且是传值调用,如果想对实参函数这边的指针改变就需要二级指针了,而free只管free掉动态开辟的空间
    1. //test.c
    2. void DListTest1()
    3. {
    4. DLNode* plist = NULL;//哨兵位
    5. DLInit(&plist);//对头节点改变需要二级指针
    6. //测试尾插
    7. DLPushBack(plist, 1);
    8. DLPushBack(plist, 2);
    9. DLPushBack(plist, 3);
    10. //DLPushFront(plist, 11);
    11. //DLPushFront(plist, 22);
    12. //DLPushFront(plist, 33);
    13. DLPrint(plist);
    14. //链表销毁
    15. DLDesTroy(plist);//销毁完后是需要置为NULL的
    16. plist = NULL;
    17. }
    1. 当然也是可以到实参这边free掉函数的,可以看下边两个例子
    1. 再举个例子
    2. 这边我们创建一个单独销毁的函数叫des,如果传二级指针是可以直接对plist这个头节点释放并且置为NULL的,确确实实可以影响到plist这个实参
    3. 下面是函数调试图,可以发现函数调用传的二级指针,拿到了plist的地址确确实实对plist实参进行了空间的释放

    1. void des(DLNode** del)
    2. {
    3. free(*del);//拿到了plist的地址,对plist的指向改变
    4. *del = NULL;
    5. }
    6. void DListTest1()
    7. {
    8. DLNode* plist = NULL;//哨兵位
    9. DLInit(&plist);//对头节点改变需要二级指针
    10. //测试尾插
    11. DLPushBack(plist, 1);
    12. DLPushBack(plist, 2);
    13. DLPushBack(plist, 3);
    14. DLPrint(plist);
    15. //链表销毁
    16. DLDesTroy(plist);
    17. des(&plist);//这边我们创建一个单独销毁的函数
    18. }
    19. int main()
    20. {
    21. DListTest1();
    22. return 0;
    23. }

    5.源码

    5.1调试部分test.c

    1. #include "DList.h"
    2. void DListTest1()
    3. {
    4. //DLNode* plist = NULL;//哨兵位
    5. //DLInit(&plist);//对头节点改变需要二级指针
    6. //第二种初始化方式
    7. DLNode* plist = DLInit();
    8. 测试尾插
    9. //DLPushBack(plist, 1);
    10. //DLPushBack(plist, 2);
    11. //DLPushBack(plist, 3);
    12. //测试头插
    13. DLPushFront(plist, 11);
    14. DLPrint(plist);
    15. DLPushFront(plist, 22);
    16. DLPrint(plist);
    17. DLPushFront(plist, 33);
    18. DLPrint(plist);
    19. //测试尾删
    20. //DLDelBack(plist);
    21. //DLPrint(plist);
    22. //DLDelBack(plist);
    23. //DLPrint(plist);
    24. //DLDelBack(plist);
    25. //DLPrint(plist);
    26. //DLDelBack(plist);
    27. //DLPrint(plist);
    28. //测试头删
    29. //DLDelFront(plist);
    30. //DLPrint(plist);
    31. //DLDelFront(plist);
    32. //DLPrint(plist);
    33. //DLDelFront(plist);
    34. //DLPrint(plist);
    35. DLNode* find = DLFind(plist, 11);// 33 22 11
    36. if (find == NULL)
    37. printf("没找到\n");
    38. else
    39. printf("找到了\n");
    40. //在pos节点后插入
    41. DLInsert(find, 99);// 33 22 11 99
    42. DLPrint(plist);
    43. //删除pos节点
    44. DLErase(find);
    45. find = NULL;
    46. DLPrint(plist);
    47. }
    48. int main()
    49. {
    50. DListTest1();
    51. return 0;
    52. }

    5.2方法实现 DList.c

    1. #include "DList.h"
    2. //新空间
    3. DLNode* DLBuyNode(DLDataType x)
    4. {
    5. DLNode* newnode = (DLNode*)malloc(sizeof(DLNode));
    6. if (newnode == NULL)
    7. {
    8. perror("malloc nwe");
    9. exit(-1);//推出整个程序
    10. }
    11. newnode->data = x;
    12. newnode->next = newnode->prev = newnode;//申请新节点,需要自循环
    13. return newnode;
    14. }
    15. //双链表的初始化
    16. //void DLInit(DLNode** pphead)//只传参不返回值,因为可以对实参直接改变
    17. //{
    18. // *pphead = DLBuyNode(-1);
    19. //}
    20. //第二种初始化方法为了保证接口的一致性,让传递的值都是一级指针
    21. DLNode* DLInit()
    22. {
    23. DLNode* node = DLBuyNode(-1);
    24. return node;
    25. }
    26. //打印链表
    27. void DLPrint(DLNode* phead)
    28. {
    29. DLNode* pcur = phead->next;//头节点指向下一个节点,也就是第一个有效节点
    30. while (pcur != phead)
    31. {
    32. printf("%d->", pcur->data);
    33. pcur = pcur->next;
    34. }
    35. printf("\n");
    36. }
    37. //尾插
    38. void DLPushBack(DLNode* phead, DLDataType x)
    39. {
    40. assert(phead);
    41. DLNode* newnode = DLBuyNode(x);
    42. //phead phead->prev newnode
    43. newnode->prev = phead->prev;//phead->prev 是头节点指向的上一个节点也就是尾节点
    44. newnode->next = phead;
    45. phead->prev->next = newnode;//尾节点指向下一个节点
    46. phead->prev = newnode;
    47. }
    48. //头插
    49. void DLPushFront(DLNode* phead, DLDataType x)
    50. {
    51. assert(phead);//头节点不能为NULL
    52. DLNode* newnode = DLBuyNode(x);
    53. newnode->next = phead->next;
    54. newnode->prev = phead;
    55. //方法1
    56. //phead->next->prev = newnode;//第一个有效节点指向newnode
    57. //phead->next = newnode;//指向下一个节点
    58. //方法2
    59. phead->next = newnode;
    60. newnode->next->prev = newnode;
    61. }
    62. //尾删
    63. void DLDelBack(DLNode* phead)
    64. {
    65. //当phead->next != phead就正常执行
    66. assert(phead && phead->next != phead);//链表都为NULL你删什么?,如果是只剩一个头结点当然也不行
    67. DLNode* del = phead->prev;
    68. del->prev->next = phead;//删除的前一个节点指向phead
    69. phead->prev = del->prev;//头结点的前一个节点指向del->prev
    70. free(del);
    71. del = NULL;
    72. }
    73. //头删
    74. void DLDelFront(DLNode* phead)
    75. {
    76. assert(phead && phead->next != phead);//如果保错就说明,要么为NULL,要么只有一个头结点
    77. DLNode* del = phead->next;//第一个有效节点
    78. phead->next = del->next;
    79. del->next->prev = phead;
    80. free(del);
    81. del = NULL;
    82. }
    83. //查找
    84. DLNode* DLFind(DLNode* phead, DLDataType x)
    85. {
    86. assert(phead);
    87. DLNode* pcur = phead->next;//可能存在多次查找,其次记得是第一个有效节点
    88. while (pcur != phead)
    89. {
    90. if (pcur->data == x)
    91. {
    92. return pcur;
    93. }
    94. pcur = pcur->next;
    95. }
    96. return NULL;
    97. }
    98. //在指定位置插入
    99. void DLInsert(DLNode* pos, DLDataType x)
    100. {
    101. assert(pos);//防止phead == NULL链表
    102. DLNode* newnode = DLBuyNode(x);
    103. //pos newnode pos->next
    104. newnode->next = pos->next;
    105. newnode->prev = pos;
    106. pos->next->prev = newnode;
    107. pos->next = newnode;
    108. }
    109. //在指定位置删除
    110. void DLErase(DLNode* pos)
    111. {
    112. assert(pos);//可能会缺少phead的检查
    113. //pos->prev pos pos->next
    114. pos->next->prev = pos->prev;
    115. pos->prev->next = pos->next;
    116. free(pos);//pos置为NULL并不影响find
    117. pos = NULL;
    118. }

    5.3声明函数和类型的部分 DList.h

    1. #pragma once
    2. #define _CRT_SECURE_NO_WARNINGS
    3. #include
    4. #include
    5. #include
    6. typedef int DLDataType;
    7. typedef struct ListNode
    8. {
    9. DLDataType data;
    10. struct ListNode* prev;//指向前一个节点
    11. struct ListNode* next;//指向下一个节点
    12. }DLNode;//链表节点
    13. //双链表初始化
    14. //void DLInit(DLNode** pphead);
    15. //第二种初始化方式
    16. DLNode* DLInit();
    17. //尾插
    18. void DLPushBack(DLNode* phead, DLDataType x);
    19. //头插
    20. void DLPushFront(DLNode* phead, DLDataType x);
    21. //尾删
    22. void DLDelBack(DLNode* phead);
    23. //头删
    24. void DLDelFront(DLNode* phead);
    25. //查找
    26. DLNode* DLFind(DLNode* phead, DLDataType x);
    27. //在指定位置插入
    28. void DLInsert(DLNode* pos, DLDataType x);
    29. //指定位置删除
    30. void DLErase(DLNode* pos);

    总结:

    这里只算是基础部分,当然最主要的是学会如何使用手里的工具

  • 相关阅读:
    【python】爬虫系列之lxml库介绍和xpath提取网页数据
    git clone:SSL: no alternative certificate subject name matches target host name
    2022.11.9 英语背诵
    分类问题和回归问题的区别是什么?
    经典动态规划问题的递归实现方法——LeetCode39 组合总和
    【PNR#2 Div1 C】图同构(图论)
    【概率论与数理统计】【线性代数】计算机保研复习
    《进阶篇第9章》学习vuex知识点后练习:求和案例_纯vue版代码
    数据结构学习:链表
    OpenSSL ca证书命令操作详解
  • 原文地址:https://blog.csdn.net/weixin_70873145/article/details/138057934