• DS线性表之链表


    前言

    我们上一期介绍了顺序表,它的底层就是数组,我们也分别对顺序表的动态版本和静态版本进行了实现!并且分析了顺序表的优缺点,优点是:尾插、尾删效率很高,其时间复杂度是O(1);缺点是:在头部插入、删除的时候效率低,其时间复杂度是O(N);而且即使是动态版本的扩容也是会浪费空间的(这里在动态内存管理介绍realloc时专门介绍过)!!这个上期的最后也介绍了!我们想有没有一种数据结构,用一个添加一个,做到最起码的空间不浪费呢?答案是:有的~!他就是我们本期介绍的链表!

    本期内容介绍

    什么是链表

    链表的分类

    单链表的实现(不带头)

    单链表的实现(带头)

    带头双向循环链表的实现

    链表和顺序表的区别

    目录

    前言

    本期内容介绍

    一、什么是链表

    二、链表的分类

    三、单链表的实现(不带头)

    链表节点类型申明

    动态申请一个节点

    链表的打印

    链表的销毁

    单链表尾插

    单链表尾删

    单链表头插

    单链表头删

    单链表查找

    在pos前插入x

    在pos后插入x

    删除pos位置

    删除pos位置的下一个

    全部源码:

    四、单链表的实现(带头)

    链表结点类型声明

    链表初始化

    链表的销毁

    链表的打印

    动态开辟一个节点

    单链表尾插

    单链表的尾删

    单链表头插

    单链表的头删

    单链表查找

    单链表修改

    在pos前插入

    在pos后插入

    删除pos位置

    删除pos后一个

    链表的长度

    总结:

    全部源码:

    五、带头双向循环链表的实现

    结点类型声明

    初始化

    销毁

    打印

    动态开辟新节点

    尾插

    尾删

    头插

    头删 

    查找

    修改

    在pos前插入

    在pos后插入

    删除pos

    删除pos的下一个

    获取长度

    六、链表和顺序表的区别

    一、什么是链表

    链表是一种物理存储结构上连续(非顺序存储的结构),逻辑结构上连续的一种存储数据的结构!它的逻辑连续是由他里面的指针实现的~!

    我们在结构体那一期介绍过一个东西叫结构体的自引用,他实质上就是链表的声明!OK,我们来个栗子回忆一下:

    1. struct Node
    2. {
    3. int data;
    4. struct Node* next;
    5. };

    这就是一个链表的申明,也就是结构体的自引用~!他这里一般把data叫做数据域,next叫做指针域!链表的指针域是指向下一个同类型的节点~!什么意思呢?我们再来画个图理解一下:

    这个图是不是很清楚的反映了上了上面的所有的点,物理结构非连续的(因为是malloc的节点在堆上,它的内存时操作系统随机分配的可能是连续的但大概率是不连续的),他们每个节点的地址都不连续,逻辑结构连续是因为当前节点的next存的是下一个节点的地址,这样只要第一个节点的地址就可以访问链表的所有节点了~!

    这就和火车一样一节连一节:

    二、链表的分类

    链表的结构有8种,是否带头节点,单双向,是否循环~的组合结果!

    在这里我们先介绍不带头的单链表,再介绍带头的单链表,最后在实现链表中最优秀的结构---》带头双向循环链表!这三种如果熟练掌握其他就都是小卡拉米~!OK,我们还是画一下,认识一下:

    不带头:

    带头:

    这就是链表的8种结构的图!其实常见的或常用的就两种无头单链表和双向循环链表!但单链表不会单独做什么存储工作,他会配合其他的去实现更高阶的数据结构例如就是实现哈希桶和图的邻接表等;

    而双向循环链表是会单独存储数据的,例如你每天刷的抖音是不是有访客呀~!它实际上底层就是双向循环链表,每次来一个方可头插一个,若一个人多次访问你的主页,也不过是改一下指针的事~!

    三、单链表的实现(不带头)

    OK,我们还是和往期一样采用多文件编程,申明等都放在.h文件里, 实现放在.c的文件, 测试在test.c中。

    一开始我们得先声明一下链表的每个节点的类型吧!

    链表节点类型申明

    1. typedef int SLDataType;
    2. typedef struct SListNode
    3. {
    4. SLDataType data;
    5. struct SListNode* next;
    6. }SListNode;

    动态申请一个节点

    这里由于是不带头的所以就不用初始化的,链表是1个或多个节点组成的,我们只要在申请的时候处理好它的next就OK!我们把next 一开始申请成功后置为NULL,这样即使是最后一个也不用单独处理为NULL了!

    1. //动态申请一个节点
    2. SListNode* BuySListNode(SLDataType x)
    3. {
    4. SListNode* newnode = (SListNode*)malloc (sizeof(SListNode));
    5. if (newnode == NULL)
    6. {
    7. perror("malloc failed");
    8. exit(-1);
    9. }
    10. newnode->data = x;
    11. newnode->next = NULL;
    12. return newnode;
    13. }

    链表的打印

    1. //打印链表
    2. void SListPrint(SListNode* head)
    3. {
    4. SListNode* cur = head;
    5. while (cur)
    6. {
    7. printf("%d->", cur->data);
    8. cur = cur->next;
    9. }
    10. printf("NULL\n");
    11. }

    链表的销毁

    为了使程序的健壮性更高,以及防止程序出现内存泄漏,我们要在操作完链表后记得销毁~!

    这里从头开始,把每一个节点free即可,但这里由于是不带头的,所以得用二级指针!!!这里head本质是形参,改变形参不改变实参,要改变形参影响实参,我们得传实参的地址!

    最后free完了记得把头置空!否则就是野指针!

    1. //单链表的销毁
    2. void SListDestory(SListNode** head)
    3. {
    4. assert(head);
    5. SListNode* cur = *head;
    6. while (cur)
    7. {
    8. SListNode* next = cur->next;
    9. free(cur);
    10. cur = next;
    11. }
    12. *head = NULL;
    13. }

    单链表尾插

    这里我们先来个硬菜不带头节点的单链表的尾插!这里还是得用二级指针,因为一开始如果链表为空第一个插入的就得是头,head就得指向他,所以改变指针的指向要用二级指针。这里head是二级指针,即使外面的链表为空他也不可能为空,因此断言一下!然后新开一个节点,判断是否是第一个插入。如果是,直接让head指向他,否则找到尾节点后插入即可~!

    1. //尾插
    2. void SListPushBack(SListNode** head, SLDataType x)
    3. {
    4. assert(head);
    5. SListNode* node = BuySListNode(x);
    6. if (*head == NULL)
    7. {
    8. *head = node;
    9. }
    10. else
    11. {
    12. SListNode* cur = *head;
    13. while (cur->next)
    14. {
    15. cur = cur->next;
    16. }
    17. cur->next = node;
    18. }
    19. }

    单链表尾删

    删除这里一开始想到的就是判断当前链表是否为空!如果为空了就不要删了,判断的方式还是和以前一样,“温柔型”和“暴力型”,小编就比较喜欢暴力型~!然后这里也要考虑删完的情况,也即是head的处理,如果把最后一个节点都删了就把head置为空,而这里的head是形参,要改变它得用它的指针也即是二级~!

    1. //尾删
    2. void SListPopBack(SListNode** head)
    3. {
    4. assert(head);
    5. assert(*head);//没有节点可删
    6. if ((*head)->next == NULL)
    7. {
    8. free(*head);
    9. *head = NULL;
    10. }
    11. else
    12. {
    13. SListNode* cur = *head;
    14. while (cur->next->next)
    15. {
    16. cur = cur->next;
    17. }
    18. free(cur->next);
    19. cur->next = NULL;
    20. }
    21. }

    当然这里常规的删除还有一种写法,即快慢指针法:用个prv前驱指针记录它的前面的一个节点。当cur->next不等于NULL时把cur赋值给prv,当cur->next为空时free掉cur,prv->next 置空即可!

    1. //尾删
    2. void SListPopBack(SListNode** head)
    3. {
    4. assert(head);
    5. assert(*head);//没有节点可删
    6. if ((*head)->next == NULL)
    7. {
    8. free(*head);
    9. *head = NULL;
    10. }
    11. else
    12. {
    13. /*SListNode* cur = *head;
    14. while (cur->next->next)
    15. {
    16. cur = cur->next;
    17. }
    18. free(cur->next);
    19. cur->next = NULL;*/
    20. SListNode* cur = *head;
    21. SListNode* prv = NULL;
    22. while (cur->next)
    23. {
    24. prv = cur;
    25. cur = cur->next;
    26. }
    27. free(cur);
    28. prv->next = NULL;
    29. }
    30. }

    OK,我们还是养成好习惯写点测试一点:

    1. void testBack()
    2. {
    3. SListNode* s = NULL;//这里不置空就是野指针,会直接奔溃掉
    4. SListPushBack(&s, 1);
    5. SListPushBack(&s, 2);
    6. SListPushBack(&s, 3);
    7. SListPushBack(&s, 4);
    8. SListPushBack(&s, 5);
    9. SListPrint(s);
    10. SListPopBack(&s);
    11. SListPrint(s);
    12. SListPopBack(&s);
    13. SListPrint(s);
    14. SListPopBack(&s);
    15. SListPrint(s);
    16. SListPopBack(&s);
    17. SListPrint(s);
    18. SListPopBack(&s);
    19. SListPrint(s);
    20. //SListPopBack(&s);//测试尾删是否有Bug
    21. //SListPrint(s);
    22. SListDestory(&s);
    23. }

    OK,尾插尾删没有问题!我们下来在来搞一下头插头删!

    单链表头插

    头插相对简单,新开的节点next连接head在让head指向新节点。但是还时要注意,一开始为空链表的情况,所以得用二级指针搞~!

    1. //头插
    2. void SListPushFront(SListNode** head, SLDataType x)
    3. {
    4. assert(head);
    5. SListNode* node = BuySListNode(x);
    6. if (*head == NULL)
    7. {
    8. *head = node;
    9. }
    10. else
    11. {
    12. node->next = *head;
    13. *head = node;
    14. }
    15. }

    单链表头删

    这里删除首先要注意为空的情况,其次只有一个节点的情况,删掉这一个节点把head置空,这里也要改变head的值所以要用二级指针。正常删除就是保存当前节点的下一个节点,free当前节点在把刚保存的下一个节点赋值给当前节点~!

    1. //头删
    2. void SListPopFront(SListNode** head)
    3. {
    4. assert(head);
    5. assert(*head);//没有节点可删
    6. if ((*head)->next == NULL)
    7. {
    8. free(*head);
    9. *head = NULL;
    10. }
    11. else
    12. {
    13. SListNode* tail = (*head)->next;
    14. free(*head);
    15. *head = tail;
    16. }
    17. }

    OK,还是来测试一下:

    1. void testFront()
    2. {
    3. SListNode* s = NULL;
    4. SListPushFront(&s, 1);
    5. SListPushFront(&s, 2);
    6. SListPushFront(&s, 3);
    7. SListPushFront(&s, 4);
    8. SListPushFront(&s, 5);
    9. SListPrint(s);
    10. SListPopFront(&s);
    11. SListPrint(s);
    12. SListPopFront(&s);
    13. SListPrint(s);
    14. SListPopFront(&s);
    15. SListPrint(s);
    16. SListPopFront(&s);
    17. SListPrint(s);
    18. SListPopFront(&s);
    19. SListPrint(s);
    20. //SListPopFront(&s);//测试头删是是否有Bug
    21. //SListPrint(s);
    22. SListDestory(&s);
    23. }

    OK,没有问题,我们在来实现一下查找和在pos前插入和pos后插入!

    单链表查找

    这里就很简单了,找到了返回该节点的地址,否则返回NULL即可!这里仅仅是查找不改变内容所以一级指针即可!

    1. //单链表的查找
    2. SListNode* SListFind(SListNode* head, SLDataType x)
    3. {
    4. SListNode* cur = head;
    5. while (cur)
    6. {
    7. if (x == cur->data)
    8. return cur;
    9. cur = cur->next;
    10. }
    11. return NULL;
    12. }

    在pos前插入x

    这里是在pos位置前插入一个x,有两种写法。这里也得注意第一个插入的情况!所以得用二级指针!第一种是直接手动实现没有借助一点外力,第二种调用了头插解决第一次插入的情况~!而且注意的是第一种你必须加break插入后跳出否则就是死循环,第二种改变不用担心这种情况~!

    1. //在pos位置前插入
    2. void SListInsert(SListNode** head, SListNode* pos, SLDataType x)
    3. {
    4. assert(head);
    5. assert(pos);
    6. SListNode* node = BuySListNode(x);
    7. if (*head == NULL)
    8. {
    9. node->next = *head;
    10. *head = node;
    11. }
    12. else
    13. {
    14. SListNode* cur = *head;
    15. while (cur)
    16. {
    17. if (cur->next == pos)
    18. {
    19. node->next = pos;
    20. cur->next = node;
    21. break;
    22. }
    23. cur = cur->next;
    24. }
    25. }
    26. }
    1. //在pos位置前插入
    2. void SListInsert(SListNode** head, SListNode* pos, SLDataType x)
    3. {
    4. assert(head);
    5. assert(pos);
    6. if (*head == NULL)
    7. {
    8. SListPushFront(head, x);
    9. }
    10. else
    11. {
    12. SListNode* cur = *head;
    13. while (cur->next != pos)
    14. {
    15. cur = cur->next;
    16. }
    17. SListNode* node = BuySListNode(x);
    18. node->next = pos;
    19. cur->next = node;
    20. }
    21. }

    在pos后插入x

    这个接口就更简单了,只需要让pos->next 连接到node->next然后再让pos->next 链接上node即可~!也不用担心pos->next 为空的情况!

    1. //在pos位置后插入
    2. void SListInsertAfter(SListNode* pos, SLDataType x)
    3. {
    4. assert(pos);
    5. SListNode* node = BuySListNode(x);
    6. node->next = pos->next;
    7. pos->next = node;
    8. }

    OK,还是来测试一下:

    1. testInsert()
    2. {
    3. SListNode* s = NULL;
    4. SListPushFront(&s, 1);
    5. SListPushFront(&s, 2);
    6. SListPushFront(&s, 3);
    7. SListPushFront(&s, 4);
    8. SListPushFront(&s, 5);
    9. SListPrint(s);
    10. SListInsert(&s, SListFind(s, 1), 10);
    11. SListPrint(s);
    12. SListInsert(&s, SListFind(s, 1), 100);
    13. SListPrint(s);
    14. SListInsertAfter(SListFind(s, 4), 40);
    15. SListPrint(s);
    16. SListDestory(&s);
    17. }

    OK,没有问题,我们在来实现一下删除pos位置和删除pos位置的后一个节点~!

    删除pos位置

    这个接口就是当头和pos相同时,可以直接调用头删不用自己多判断。否则就遍历找pos然后删除~~!

    1. //删除pos位置
    2. void SListErase(SListNode** head, SListNode* pos)
    3. {
    4. assert(head);
    5. assert(*head);//空了就不要删了
    6. assert(pos);
    7. if (*head == pos)
    8. {
    9. SListPopFront(head);
    10. }
    11. else
    12. {
    13. SListNode* cur = *head;
    14. while (cur->next != pos)
    15. {
    16. cur = cur->next;
    17. }
    18. cur->next = pos->next;
    19. free(pos);
    20. }
    21. }

    删除pos位置的下一个

    这个得保证有pos和有pos的下一个,然后保存pos的下一个,pos的next 连接上pos->next->next 然后删掉保存的pos的下一个即可~!

    1. //删除pos后一个位置
    2. void SListEraseAfter(SListNode* pos)
    3. {
    4. assert(pos);
    5. assert(pos->next);
    6. SListNode* Pnext = pos->next;
    7. pos->next = Pnext->next;
    8. free(Pnext);
    9. }

    OK,测试一下:

    1. void testErase()
    2. {
    3. SListNode* s = NULL;//这里不置空就是野指针,会直接奔溃掉
    4. SListPushBack(&s, 1);
    5. SListPushBack(&s, 2);
    6. SListPushBack(&s, 3);
    7. SListPushBack(&s, 4);
    8. SListPushBack(&s, 5);
    9. SListPrint(s);
    10. SListErase(&s, SListFind(s, 1));
    11. SListPrint(s);
    12. SListErase(&s, SListFind(s, 2));
    13. SListPrint(s);
    14. SListEraseAfter(SListFind(s, 3));
    15. SListPrint(s);
    16. SListDestory(&s);
    17. }

    OK,没有问题~!

    全部源码:

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. typedef int SLDataType;
    6. typedef struct SListNode
    7. {
    8. SLDataType data;
    9. struct SListNode* next;
    10. }SListNode;
    11. //动态申请一个节点
    12. SListNode* BuySListNode(SLDataType x);
    13. //打印链表
    14. void SListPrint(SListNode* head);
    15. //单链表的销毁
    16. void SListDestory(SListNode** head);
    17. //尾插
    18. void SListPushBack(SListNode** head, SLDataType x);
    19. //尾删
    20. void SListPopBack(SListNode** head);
    21. //头插
    22. void SListPushFront(SListNode** head, SLDataType x);
    23. //头删
    24. void SListPopFront(SListNode** head);
    25. //单链表的查找
    26. SListNode* SListFind(SListNode* head, SLDataType x);
    27. //在pos位置前插入
    28. void SListInsert(SListNode** head, SListNode* pos, SLDataType x);
    29. //在pos位置后插入
    30. void SListInsertAfter(SListNode* pos, SLDataType x);
    31. //删除pos位置
    32. void SListErase(SListNode** head, SListNode* pos);
    33. //删除pos后一个位置
    34. void SListEraseAfter(SListNode* pos);
    1. #include "SListNode.h"
    2. //动态申请一个节点
    3. SListNode* BuySListNode(SLDataType x)
    4. {
    5. SListNode* newnode = (SListNode*)malloc (sizeof(SListNode));
    6. if (newnode == NULL)
    7. {
    8. perror("malloc failed");
    9. exit(-1);
    10. }
    11. newnode->data = x;
    12. newnode->next = NULL;
    13. return newnode;
    14. }
    15. //打印链表
    16. void SListPrint(SListNode* head)
    17. {
    18. SListNode* cur = head;
    19. while (cur)
    20. {
    21. printf("%d->", cur->data);
    22. cur = cur->next;
    23. }
    24. printf("NULL\n");
    25. }
    26. //单链表的销毁
    27. void SListDestory(SListNode** head)
    28. {
    29. assert(head);
    30. SListNode* cur = *head;
    31. while (cur)
    32. {
    33. SListNode* next = cur->next;
    34. free(cur);
    35. cur = next;
    36. }
    37. *head = NULL;
    38. }
    39. //尾插
    40. void SListPushBack(SListNode** head, SLDataType x)
    41. {
    42. assert(head);
    43. SListNode* node = BuySListNode(x);
    44. if (*head == NULL)
    45. {
    46. *head = node;
    47. }
    48. else
    49. {
    50. SListNode* cur = *head;
    51. while (cur->next)
    52. {
    53. cur = cur->next;
    54. }
    55. cur->next = node;
    56. }
    57. }
    58. //尾删
    59. void SListPopBack(SListNode** head)
    60. {
    61. assert(head);
    62. assert(*head);//没有节点可删
    63. if ((*head)->next == NULL)
    64. {
    65. free(*head);
    66. *head = NULL;
    67. }
    68. else
    69. {
    70. /*SListNode* cur = *head;
    71. while (cur->next->next)
    72. {
    73. cur = cur->next;
    74. }
    75. free(cur->next);
    76. cur->next = NULL;*/
    77. SListNode* cur = *head;
    78. SListNode* prv = NULL;
    79. while (cur->next)
    80. {
    81. prv = cur;
    82. cur = cur->next;
    83. }
    84. free(cur);
    85. prv->next = NULL;
    86. }
    87. }
    88. //头插
    89. void SListPushFront(SListNode** head, SLDataType x)
    90. {
    91. assert(head);
    92. SListNode* node = BuySListNode(x);
    93. if (*head == NULL)
    94. {
    95. *head = node;
    96. }
    97. else
    98. {
    99. node->next = *head;
    100. *head = node;
    101. }
    102. }
    103. //头删
    104. void SListPopFront(SListNode** head)
    105. {
    106. assert(head);
    107. assert(*head);//没有节点可删
    108. if ((*head)->next == NULL)
    109. {
    110. free(*head);
    111. *head = NULL;
    112. }
    113. else
    114. {
    115. SListNode* tail = (*head)->next;
    116. free(*head);
    117. *head = tail;
    118. }
    119. }
    120. //单链表的查找
    121. SListNode* SListFind(SListNode* head, SLDataType x)
    122. {
    123. SListNode* cur = head;
    124. while (cur)
    125. {
    126. if (x == cur->data)
    127. return cur;
    128. cur = cur->next;
    129. }
    130. return NULL;
    131. }
    132. //在pos位置前插入
    133. void SListInsert(SListNode** head, SListNode* pos, SLDataType x)
    134. {
    135. assert(head);
    136. assert(pos);
    137. SListNode* node = BuySListNode(x);
    138. if (*head == NULL)
    139. {
    140. node->next = *head;
    141. *head = node;
    142. }
    143. else
    144. {
    145. SListNode* cur = *head;
    146. while (cur)
    147. {
    148. if (cur->next == pos)
    149. {
    150. node->next = pos;
    151. cur->next = node;
    152. break;
    153. }
    154. cur = cur->next;
    155. }
    156. }
    157. }
    158. //在pos位置后插入
    159. void SListInsertAfter(SListNode* pos, SLDataType x)
    160. {
    161. assert(pos);
    162. SListNode* node = BuySListNode(x);
    163. node->next = pos->next;
    164. pos->next = node;
    165. }
    166. //删除pos位置
    167. void SListErase(SListNode** head, SListNode* pos)
    168. {
    169. assert(head);
    170. assert(*head);//空了就不要删了
    171. assert(pos);
    172. if (*head == pos)
    173. {
    174. SListPopFront(head);
    175. }
    176. else
    177. {
    178. SListNode* cur = *head;
    179. while (cur->next != pos)
    180. {
    181. cur = cur->next;
    182. }
    183. cur->next = pos->next;
    184. free(pos);
    185. }
    186. }
    187. //删除pos后一个位置
    188. void SListEraseAfter(SListNode* pos)
    189. {
    190. assert(pos);
    191. assert(pos->next);
    192. SListNode* Pnext = pos->next;
    193. pos->next = Pnext->next;
    194. free(Pnext);
    195. }
    1. #include "SListNode.h"
    2. void testBack()
    3. {
    4. SListNode* s = NULL;//这里不置空就是野指针,会直接奔溃掉
    5. SListPushBack(&s, 1);
    6. SListPushBack(&s, 2);
    7. SListPushBack(&s, 3);
    8. SListPushBack(&s, 4);
    9. SListPushBack(&s, 5);
    10. SListPrint(s);
    11. SListPopBack(&s);
    12. SListPrint(s);
    13. SListPopBack(&s);
    14. SListPrint(s);
    15. SListPopBack(&s);
    16. SListPrint(s);
    17. SListPopBack(&s);
    18. SListPrint(s);
    19. SListPopBack(&s);
    20. SListPrint(s);
    21. //SListPopBack(&s);//测试尾删是否有Bug
    22. //SListPrint(s);
    23. SListDestory(&s);
    24. }
    25. void testFront()
    26. {
    27. SListNode* s = NULL;
    28. SListPushFront(&s, 1);
    29. SListPushFront(&s, 2);
    30. SListPushFront(&s, 3);
    31. SListPushFront(&s, 4);
    32. SListPushFront(&s, 5);
    33. SListPrint(s);
    34. SListPopFront(&s);
    35. SListPrint(s);
    36. SListPopFront(&s);
    37. SListPrint(s);
    38. SListPopFront(&s);
    39. SListPrint(s);
    40. SListPopFront(&s);
    41. SListPrint(s);
    42. SListPopFront(&s);
    43. SListPrint(s);
    44. //SListPopFront(&s);//测试头删是是否有Bug
    45. //SListPrint(s);
    46. SListDestory(&s);
    47. }
    48. testInsert()
    49. {
    50. SListNode* s = NULL;
    51. SListPushFront(&s, 1);
    52. SListPushFront(&s, 2);
    53. SListPushFront(&s, 3);
    54. SListPushFront(&s, 4);
    55. SListPushFront(&s, 5);
    56. SListPrint(s);
    57. SListInsert(&s, SListFind(s, 1), 10);
    58. SListPrint(s);
    59. SListInsert(&s, SListFind(s, 1), 100);
    60. SListPrint(s);
    61. SListInsertAfter(SListFind(s, 4), 40);
    62. SListPrint(s);
    63. SListDestory(&s);
    64. }
    65. void testErase()
    66. {
    67. SListNode* s = NULL;//这里不置空就是野指针,会直接奔溃掉
    68. SListPushBack(&s, 1);
    69. SListPushBack(&s, 2);
    70. SListPushBack(&s, 3);
    71. SListPushBack(&s, 4);
    72. SListPushBack(&s, 5);
    73. SListPrint(s);
    74. SListErase(&s, SListFind(s, 1));
    75. SListPrint(s);
    76. SListErase(&s, SListFind(s, 2));
    77. SListPrint(s);
    78. SListEraseAfter(SListFind(s, 3));
    79. SListPrint(s);
    80. SListDestory(&s);
    81. }
    82. int main()
    83. {
    84. //testBack();
    85. //testFront();
    86. //testInsert();
    87. testErase();
    88. return 0;
    89. }

    OK,无头(没有哨兵位的头结点)单链表就到这里~!这应该是我们介绍的三个里面最复杂的一个~!如果这个掌握了那其他的很容易~!下面我们来看看带哨兵位的头结点的单链表~!

    四、单链表的实现(带头)

    还是和没有头的一样先声明每个节点的类型:

    链表结点类型声明

    1. typedef int SLDataType;
    2. typedef struct SListNode
    3. {
    4. SLDataType data;
    5. struct SListNode* next;
    6. }SLNode;

    链表初始化

    由于前面是不带头的,所以不需要进行初始化,而这里现在是带哨兵位头结点的~!一开始得要处理好头结点~!处理的方式有两种,一种是在初始的时候无返回值需要传一次二级,其他地方都不用,这显得有点突兀,所以我们采用第二种返回值,将处理好的头结点的地址返回外面接收此时就是带头的了。其实还有一种就是再定义一个结构体,里面是专门维护这个链表的指针,传一级指针也是可以的~~!当然这里采用返回值的方式!

    1. //链表的初始化
    2. SLNode* SLInit()
    3. {
    4. SLNode* head = (SLNode*)malloc(sizeof(SLNode));
    5. if (head == NULL)
    6. {
    7. perror("head malloc failed");
    8. exit(-1);
    9. }
    10. head->next = NULL;
    11. return head;
    12. }

    链表的销毁

    这里销毁也是一样有两种方式一种是传二级啥都不管了,内部会处理好但这里人家都是一级你一个二级显得有些突兀,还有一种就是不传二级外面最后记得把头置空即可!我们这里就采用第二种!

    1. //链表的销毁
    2. void SLDestory(SLNode* head)
    3. {
    4. assert(head);//头结点不可能为空
    5. SLNode* cur = head;
    6. while (cur)
    7. {
    8. SLNode* tail = cur->next;
    9. free(cur);
    10. cur = tail;
    11. }
    12. }

    链表的打印

    1. //链表的打印
    2. void SLPrint(SLNode* head)
    3. {
    4. assert(head);
    5. SLNode* cur = head->next;
    6. while (cur)
    7. {
    8. printf("%d->", cur->data);
    9. cur = cur->next;
    10. }
    11. printf("NULL\n");
    12. }

    动态开辟一个节点

    由于后面会有大量的插入,每次插入都要开新节点,所以我们直接把他封装成一个函数,后续调用起来方便,代码简洁~!

    1. //动态开辟一个节点
    2. SLNode* BuyNode(SLDataType x)
    3. {
    4. SLNode* node = (SLNode*)malloc(sizeof(SLNode));
    5. if (node == NULL)
    6. {
    7. perror("malloc failed");
    8. exit(-1);
    9. }
    10. node->data = x;
    11. node->next = NULL;
    12. return node;
    13. }

    单链表尾插

    由于有头结点,可以不因考虑第一个插入的情况,另外这里的head是头结点,即使链表为空他也不可能为空!因此为了代码的健壮性断言一下还是好的~!

    1. //尾插
    2. void SLPushBack(SLNode* head, SLDataType x)
    3. {
    4. assert(head);
    5. SLNode* newnode = BuyNode(x);
    6. SLNode* cur = head;
    7. while (cur->next)
    8. {
    9. cur = cur->next;
    10. }
    11. cur->next = newnode;
    12. }

    单链表的尾删

    尾删也不许担心删完后置头结点为空,这里带了哨兵位的头不需要担心直接删即可,另外这里还是有两种方法上面已经说过,这里采用快慢指针法~!

    1. //尾删
    2. void SLPopBack(SLNode* head)
    3. {
    4. assert(head);
    5. assert(head->next);//空链表
    6. SLNode* cur = head;
    7. while (cur->next->next)
    8. {
    9. cur = cur->next;
    10. }
    11. free(cur->next);
    12. cur->next = NULL;
    13. }

    OK,来测试一把:

    1. void testBack()
    2. {
    3. SLNode* s = SLInit();
    4. SLPushBack(s, 1);
    5. SLPushBack(s, 2);
    6. SLPushBack(s, 3);
    7. SLPrint(s);
    8. SLPopBack(s);
    9. SLPrint(s);
    10. SLPopBack(s);
    11. SLPrint(s);
    12. SLPopBack(s);
    13. SLPrint(s);
    14. //SLPopBack(s);//检测尾删是否有Bug
    15. //SLPrint(s);
    16. SLDestory(s);
    17. s = NULL;//记得把头置空,避免野指针
    18. }

    OK,没有问题,这个是不是相较于上面没带哨兵位的头结点的那个简单多了~!哈哈,OK,我们再来实现一下,头插、头删~!

    单链表头插

    这相比上面的就很简单了,直接插在哨兵位头结点的后面即可!不用考虑第一个插入改变头的情况~!

    1. //头插
    2. void SLPushFront(SLNode* head, SLDataType x)
    3. {
    4. assert(head);
    5. SLNode* newnode = BuyNode(x);
    6. newnode->next = head->next;
    7. head->next = newnode;
    8. }

    单链表的头删

    头删也是比以前的简单,只需判空即可,不需要考虑改变头的问题~!

    1. //头删
    2. void SLPopFront(SLNode* head)
    3. {
    4. assert(head);
    5. assert(head->next);//无节点可删
    6. SLNode* del = head->next;
    7. head->next = del->next;
    8. free(del);
    9. }

    OK,还是来测试一波:

    1. void testFront()
    2. {
    3. SLNode* s = SLInit();
    4. SLPushFront(s, 1);
    5. SLPushFront(s, 2);
    6. SLPushFront(s, 3);
    7. SLPushFront(s, 4);
    8. SLPrint(s);
    9. SLPopFront(s);
    10. SLPrint(s);
    11. SLPopFront(s);
    12. SLPrint(s);
    13. SLPopFront(s);
    14. SLPrint(s);
    15. SLPopFront(s);
    16. SLPrint(s);
    17. //SLPopFront(s);//测试头删是否有Bug
    18. //SLPrint(s);
    19. SLDestory(s);
    20. s = NULL;//记得把头置空,避免野指针
    21. }

    单链表查找

    还是和前面一样找到了返回当前节点的地址,否则返回NULL。

    1. //单链表的查找
    2. SLNode* SLFind(SLNode* head, SLDataType x)
    3. {
    4. assert(head);
    5. SLNode* cur = head->next;
    6. while (cur)
    7. {
    8. if (x == cur->data)
    9. return cur;
    10. cur = cur->next;
    11. }
    12. return NULL;
    13. }

    这里肯定有小伙伴问这个函数不就查找吗,除了配合insert和erase插入删除还能干啥?其实他还能修改信息~!数据结构学的是数据的物理存储结构、逻辑顺序结构和运算~!这里的运算简单点就是增删查改。上面无头的我们已经进行了增删查,没有实现改(其实是我忘了,刚刚想起哈哈),这里我们可以补在这个上,上面无头的也是一样的~!!!

    单链表修改

    1. //单链表的修改
    2. void SLModif(SLNode* head, SLDataType x, SLDataType y)
    3. {
    4. assert(head);
    5. SLNode* ret = SLFind(head, x);
    6. assert(ret);
    7. ret->data = y;
    8. }

    在pos前插入

    逻辑和上面的一样,这里也不用考虑第一个插入要改变头的情况~!

    1. //在pos前插入
    2. void SLInsert(SLNode* head, SLNode* pos, SLDataType x)
    3. {
    4. assert(head);
    5. assert(pos);
    6. SLNode* newnode = BuyNode(x);
    7. SLNode* cur = head;
    8. while (cur->next != pos)
    9. {
    10. cur = cur->next;
    11. }
    12. newnode->next = pos;
    13. cur->next = newnode;
    14. }

    在pos后插入

    1. //在pos后插入
    2. void SLInsertAfter(SLNode* pos, SLDataType x)
    3. {
    4. assert(pos);
    5. SLNode* newnode = BuyNode(x);
    6. newnode->next = pos->next;
    7. pos->next = newnode;
    8. }

    OK,则是一波走起:(这里就把修改顺便测了)

    1. void testInsert()
    2. {
    3. SLNode* s = SLInit();
    4. SLPushBack(s, 1);
    5. SLPushBack(s, 2);
    6. SLPushBack(s, 3);
    7. SLPrint(s);
    8. SLInsert(s, SLFind(s, 1), 10);
    9. SLPrint(s);
    10. SLInsertAfter(SLFind(s, 2), 200);
    11. SLPrint(s);
    12. SLModif(s, 3, 300);
    13. SLPrint(s);
    14. SLDestory(s);
    15. s = NULL;//记得把头置空,避免野指针
    16. }

    OK,没有问题~!再来实现两个erase的接口!

    删除pos位置

    这里由于是SLFind配合删除的,所以不因担心删完多删的情况!

    1. //删除pos位置
    2. void SLErase(SLNode* head, SLNode* pos)
    3. {
    4. assert(head);
    5. assert(pos);
    6. SLNode* cur = head;
    7. while (cur->next != pos)
    8. {
    9. cur = cur->next;
    10. }
    11. cur->next = pos->next;
    12. free(pos);
    13. }

    删除pos后一个

    唯一要注意的是pos得有下一个才让删除,否则结束~!

    1. //删除pos位置的后一个
    2. void SLEraseAfter(SLNode* pos)
    3. {
    4. assert(pos);
    5. assert(pos->next);
    6. SLNode* del = pos->next;
    7. pos->next = del->next;
    8. free(del);
    9. }

    链表的长度

    这里多加的一个接口是获取链表的长度~!这个不难很容易~!

    1. //获取链表的长度
    2. int SLSize(SLNode* head)
    3. {
    4. assert(head);
    5. int size = 0;
    6. SLNode* cur = head->next;
    7. while (cur)
    8. {
    9. ++size;
    10. cur = cur->next;
    11. }
    12. return size;
    13. }

    OK,测试一把:

    1. void testErase()
    2. {
    3. SLNode* s = SLInit();
    4. SLPushBack(s, 1);
    5. SLPushBack(s, 2);
    6. SLPushBack(s, 3);
    7. SLPushBack(s, 4);
    8. SLPrint(s);
    9. SLErase(s, SLFind(s, 1));
    10. SLPrint(s);
    11. SLEraseAfter(SLFind(s, 2));
    12. SLPrint(s);
    13. printf("size = %d\n", SLSize(s));
    14. SLDestory(s);
    15. s = NULL;//记得把头置空,避免野指针
    16. }

    OK,没有问题~!

    总结:

    带哨兵位的头结点的确比不带头的要简单很多!这两种都很重要以后工作中常用的就是这两种和下面要介绍的带头双向循环链表~!而且他这里插入和删除可以用Insert和Erase简化!

    全部源码:

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. typedef int SLDataType;
    6. typedef struct SListNode
    7. {
    8. SLDataType data;
    9. struct SListNode* next;
    10. }SLNode;
    11. //链表的初始化
    12. SLNode* SLInit();
    13. //链表的销毁
    14. void SLDestory(SLNode* head);
    15. //链表的打印
    16. void SLPrint(SLNode* head);
    17. //动态开辟一个节点
    18. SLNode* BuyNode(SLDataType x);
    19. //尾插
    20. void SLPushBack(SLNode* head, SLDataType x);
    21. //尾删
    22. void SLPopBack(SLNode* head);
    23. //头插
    24. void SLPushFront(SLNode* head, SLDataType x);
    25. //头删
    26. void SLPopFront(SLNode* head);
    27. //单链表的查找
    28. SLNode* SLFind(SLNode* head, SLDataType x);
    29. //单链表的修改
    30. void SLModif(SLNode* head, SLDataType x, SLDataType y);
    31. //在pos前插入
    32. void SLInsert(SLNode* head, SLNode* pos, SLDataType x);
    33. //在pos后插入
    34. void SLInsertAfter(SLNode* pos, SLDataType x);
    35. //删除pos位置
    36. void SLErase(SLNode* head, SLNode* pos);
    37. //删除pos位置的后一个
    38. void SLEraseAfter(SLNode* pos);
    39. //获取链表的长度
    40. int SLSize(SLNode* head);
    1. #include "SLT.h"
    2. //链表的初始化
    3. SLNode* SLInit()
    4. {
    5. SLNode* head = (SLNode*)malloc(sizeof(SLNode));
    6. if (head == NULL)
    7. {
    8. perror("head malloc failed");
    9. exit(-1);
    10. }
    11. head->next = NULL;
    12. return head;
    13. }
    14. //链表的销毁
    15. void SLDestory(SLNode* head)
    16. {
    17. assert(head);//头结点不可能为空
    18. SLNode* cur = head;
    19. while (cur)
    20. {
    21. SLNode* tail = cur->next;
    22. free(cur);
    23. cur = tail;
    24. }
    25. }
    26. //链表的打印
    27. void SLPrint(SLNode* head)
    28. {
    29. assert(head);
    30. SLNode* cur = head->next;
    31. while (cur)
    32. {
    33. printf("%d->", cur->data);
    34. cur = cur->next;
    35. }
    36. printf("NULL\n");
    37. }
    38. //动态开辟一个节点
    39. SLNode* BuyNode(SLDataType x)
    40. {
    41. SLNode* node = (SLNode*)malloc(sizeof(SLNode));
    42. if (node == NULL)
    43. {
    44. perror("malloc failed");
    45. exit(-1);
    46. }
    47. node->data = x;
    48. node->next = NULL;
    49. return node;
    50. }
    51. //尾插
    52. void SLPushBack(SLNode* head, SLDataType x)
    53. {
    54. assert(head);
    55. SLNode* newnode = BuyNode(x);
    56. SLNode* cur = head;
    57. while (cur->next)
    58. {
    59. cur = cur->next;
    60. }
    61. cur->next = newnode;
    62. }
    63. //尾删
    64. void SLPopBack(SLNode* head)
    65. {
    66. assert(head);
    67. assert(head->next);//空链表
    68. SLNode* cur = head;
    69. while (cur->next->next)
    70. {
    71. cur = cur->next;
    72. }
    73. free(cur->next);
    74. cur->next = NULL;
    75. }
    76. //头插
    77. void SLPushFront(SLNode* head, SLDataType x)
    78. {
    79. assert(head);
    80. SLNode* newnode = BuyNode(x);
    81. newnode->next = head->next;
    82. head->next = newnode;
    83. }
    84. //头删
    85. void SLPopFront(SLNode* head)
    86. {
    87. assert(head);
    88. assert(head->next);//无节点可删
    89. SLNode* del = head->next;
    90. head->next = del->next;
    91. free(del);
    92. }
    93. //单链表的查找
    94. SLNode* SLFind(SLNode* head, SLDataType x)
    95. {
    96. assert(head);
    97. SLNode* cur = head->next;
    98. while (cur)
    99. {
    100. if (x == cur->data)
    101. return cur;
    102. cur = cur->next;
    103. }
    104. return NULL;
    105. }
    106. //单链表的修改
    107. void SLModif(SLNode* head, SLDataType x, SLDataType y)
    108. {
    109. assert(head);
    110. SLNode* ret = SLFind(head, x);
    111. assert(ret);
    112. ret->data = y;
    113. }
    114. //在pos前插入
    115. void SLInsert(SLNode* head, SLNode* pos, SLDataType x)
    116. {
    117. assert(head);
    118. assert(pos);
    119. SLNode* newnode = BuyNode(x);
    120. SLNode* cur = head;
    121. while (cur->next != pos)
    122. {
    123. cur = cur->next;
    124. }
    125. newnode->next = pos;
    126. cur->next = newnode;
    127. }
    128. //在pos后插入
    129. void SLInsertAfter(SLNode* pos, SLDataType x)
    130. {
    131. assert(pos);
    132. SLNode* newnode = BuyNode(x);
    133. newnode->next = pos->next;
    134. pos->next = newnode;
    135. }
    136. //删除pos位置
    137. void SLErase(SLNode* head, SLNode* pos)
    138. {
    139. assert(head);
    140. assert(pos);
    141. SLNode* cur = head;
    142. while (cur->next != pos)
    143. {
    144. cur = cur->next;
    145. }
    146. cur->next = pos->next;
    147. free(pos);
    148. }
    149. //删除pos位置的后一个
    150. void SLEraseAfter(SLNode* pos)
    151. {
    152. assert(pos);
    153. assert(pos->next);
    154. SLNode* del = pos->next;
    155. pos->next = del->next;
    156. free(del);
    157. }
    158. //获取链表的长度
    159. int SLSize(SLNode* head)
    160. {
    161. assert(head);
    162. int size = 0;
    163. SLNode* cur = head->next;
    164. while (cur)
    165. {
    166. ++size;
    167. cur = cur->next;
    168. }
    169. return size;
    170. }
    1. #include "SLT.h"
    2. void testBack()
    3. {
    4. SLNode* s = SLInit();
    5. SLPushBack(s, 1);
    6. SLPushBack(s, 2);
    7. SLPushBack(s, 3);
    8. SLPrint(s);
    9. SLPopBack(s);
    10. SLPrint(s);
    11. SLPopBack(s);
    12. SLPrint(s);
    13. SLPopBack(s);
    14. SLPrint(s);
    15. //SLPopBack(s);//检测尾删是否有Bug
    16. //SLPrint(s);
    17. SLDestory(s);
    18. s = NULL;//记得把头置空,避免野指针
    19. }
    20. void testFront()
    21. {
    22. SLNode* s = SLInit();
    23. SLPushFront(s, 1);
    24. SLPushFront(s, 2);
    25. SLPushFront(s, 3);
    26. SLPushFront(s, 4);
    27. SLPrint(s);
    28. SLPopFront(s);
    29. SLPrint(s);
    30. SLPopFront(s);
    31. SLPrint(s);
    32. SLPopFront(s);
    33. SLPrint(s);
    34. SLPopFront(s);
    35. SLPrint(s);
    36. //SLPopFront(s);//测试头删是否有Bug
    37. //SLPrint(s);
    38. SLDestory(s);
    39. s = NULL;//记得把头置空,避免野指针
    40. }
    41. void testInsert()
    42. {
    43. SLNode* s = SLInit();
    44. SLPushBack(s, 1);
    45. SLPushBack(s, 2);
    46. SLPushBack(s, 3);
    47. SLPrint(s);
    48. SLInsert(s, SLFind(s, 1), 10);
    49. SLPrint(s);
    50. SLInsertAfter(SLFind(s, 2), 200);
    51. SLPrint(s);
    52. SLModif(s, 3, 300);
    53. SLPrint(s);
    54. SLDestory(s);
    55. s = NULL;//记得把头置空,避免野指针
    56. }
    57. void testErase()
    58. {
    59. SLNode* s = SLInit();
    60. SLPushBack(s, 1);
    61. SLPushBack(s, 2);
    62. SLPushBack(s, 3);
    63. SLPushBack(s, 4);
    64. SLPrint(s);
    65. SLErase(s, SLFind(s, 1));
    66. SLPrint(s);
    67. SLEraseAfter(SLFind(s, 2));
    68. SLPrint(s);
    69. printf("size = %d\n", SLSize(s));
    70. SLDestory(s);
    71. s = NULL;//记得把头置空,避免野指针
    72. }
    73. int main()
    74. {
    75. //testBack();
    76. //testFront();
    77. //testInsert();
    78. testErase();
    79. return 0;
    80. }

    五、带头双向循环链表的实现

    我们上面实现的两种都是单链表,他们头插头删的效率很高,但在中间或尾部插入删除时的效率就很低,为了解决这个问题我们在定义节点类型时在加入一个前驱指针来记录当前节点的前一个节点的地址,这样插入删除就很OK了~!当最后一个时让他的后继指针指向头结点,第一个的前驱指向最后一个节点,这样就实现了循环而且任何节点的插入删除都很高~!OK,下面是图:

    OK,下面我们就来实现一下~!

    结点类型声明

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

    初始化

    1. //初始化
    2. ListNode* ListInit()
    3. {
    4. ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    5. if (!head)
    6. {
    7. perror("malloc failed");
    8. exit(-1);
    9. }
    10. head->prv = head;
    11. head->next = head;
    12. return head;
    13. }

    销毁

    这里与上面单向带有不一样的是!这里因为是循环的最后的next指向哨兵位的头,所以不能和上面一样直接从head开始销毁,应该从head->next开始等所有的销毁完了,在把head置空即可~!这是因为是一级指针所以外面的调用者在最后记得置空head~!

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

    打印

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

    动态开辟新节点

    1. //动态开辟一个节点
    2. ListNode* BuyNode(LTDataType x)
    3. {
    4. ListNode* node = (ListNode*)malloc(sizeof(ListNode));
    5. if (!node)
    6. {
    7. perror("malloc failed");
    8. exit(-1);
    9. }
    10. node->data = x;
    11. node->prv = NULL;
    12. node->next = NULL;
    13. return node;
    14. }

    尾插

    这里比单链表好的就是不用遍历找尾直接就可以找到,但注意的是要把prv前驱指针处理好~!如果不熟练可以多定义个指针,如果熟悉了就可以多个->next->prv哈哈~!下面是两种写法!

    1. //尾插
    2. void ListPushBack(ListNode* head, LTDataType x)
    3. {
    4. assert(head);
    5. ListNode* newnode = BuyNode(x);
    6. ListNode* tail = head->prv;
    7. tail->next = newnode;
    8. newnode->prv = tail;
    9. newnode->next = head;
    10. head->prv = newnode;
    11. /*head->prv->next = newnode;
    12. newnode->prv = head->prv;
    13. newnode->next = head;
    14. head->prv = newnode;*/
    15. }

    尾删

    尾删也是一样可以多定义指针也可以直接来!也要注意处理好前驱~!下面是两种方式!

    1. //尾删
    2. void ListPopBack(ListNode* head)
    3. {
    4. assert(head);
    5. assert(head->next != head);//链表为空
    6. ListNode* tail = head->prv;
    7. ListNode* tailPrv = tail->prv;
    8. tailPrv->next = head;
    9. head->prv = tailPrv;
    10. free(tail);
    11. /*ListNode* del = head->prv;
    12. head->prv->prv->next = head;
    13. head->prv = head->prv->prv;
    14. free(del);*/
    15. }

    OK还是测试一波:

    1. void testBack()
    2. {
    3. ListNode* L = ListInit();
    4. ListPushBack(L, 1);
    5. ListPushBack(L, 2);
    6. ListPushBack(L, 3);
    7. ListPrint(L);
    8. ListPopBack(L);
    9. ListPrint(L);
    10. ListPopBack(L);
    11. ListPrint(L);
    12. ListPopBack(L);
    13. ListPrint(L);
    14. //ListPopBack(L);//测试尾删是否有Bug
    15. //ListPrint(L);
    16. ListDestory(L);
    17. L = NULL;
    18. }

    OK,没有问题,再来搞两个头插和头删~!

    头插

    1. //头插
    2. void ListPushFront(ListNode* head, LTDataType x)
    3. {
    4. assert(head);
    5. ListNode* newnode = BuyNode(x);
    6. ListNode* tail = head->next;
    7. newnode->next = tail;
    8. tail->prv = newnode;
    9. head->next = newnode;
    10. newnode->prv = head;
    11. /*newnode->next = head->next;
    12. head->next->prv = newnode;
    13. head->next = newnode;
    14. newnode->prv = head;*/
    15. }

    头删 

    1. //头删
    2. void ListPopFront(ListNode* head)
    3. {
    4. assert(head);
    5. assert(head->next != head);
    6. ListNode* del = head->next;
    7. head->next = del->next;
    8. del->next->prv = head;
    9. free(del);
    10. }

    OK,来测试一波:

    1. void testFront()
    2. {
    3. ListNode* L = ListInit();
    4. ListPushFront(L, 1);
    5. ListPushFront(L, 2);
    6. ListPushFront(L, 3);
    7. ListPrint(L);
    8. ListPopFront(L);
    9. ListPrint(L);
    10. ListPopFront(L);
    11. ListPrint(L);
    12. ListPopFront(L);
    13. ListPrint(L);
    14. //ListPopFront(L);//测试头删是否有Bug
    15. //ListPrint(L);
    16. ListDestory(L);
    17. L = NULL;
    18. }

    OK,没有bug!我们继续:

    查找

    这里还是和前面一样找到了返回当前节点的地址否则返回NULL,实际上在很多常见下就是这样的~!

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

    修改

    记得判断找不到的情况,我这里采用的是暴力检查!!!

    1. //修改
    2. void ListModif(ListNode* head, LTDataType x, LTDataType y)
    3. {
    4. assert(head);
    5. ListNode* ret = ListFind(head, x);
    6. assert(ret);
    7. ret->data = y;
    8. }

    在pos前插入

    这里注意的是由于查找函数已经把pos查找好了,且有前驱指针所以不需要遍历找pos,直接链接即可~!

    1. //在pos前插入
    2. void ListInsert( ListNode* pos, LTDataType x)
    3. {
    4. assert(pos);
    5. ListNode* newnode = BuyNode(x);
    6. ListNode* Prv = pos->prv;
    7. newnode->next = pos;
    8. pos->prv = newnode;
    9. Prv->next = newnode;
    10. newnode->prv = Prv;
    11. }

    在pos后插入

    1. //在pos后插入
    2. void ListInsertAfter(ListNode* pos, LTDataType x)
    3. {
    4. assert(pos);
    5. ListNode* newnode = BuyNode(x);
    6. ListNode* tail = pos->next;
    7. newnode->next = tail;
    8. tail->prv = newnode;
    9. pos->next = newnode;
    10. newnode->prv = pos;
    11. }

    OK,测试一波:

    1. void testInsert()
    2. {
    3. ListNode* L = ListInit();
    4. ListPushBack(L, 1);
    5. ListPushBack(L, 2);
    6. ListPushBack(L, 3);
    7. ListPrint(L);
    8. ListInsert(ListFind(L, 1), 10);
    9. ListPrint(L);
    10. ListInsertAfter(ListFind(L, 3), 300);
    11. ListPrint(L);
    12. ListDestory(L);
    13. L = NULL;
    14. }

    OK,没有问题~!

    删除pos

    1. //删除pos位置
    2. void ListErase(ListNode* pos)
    3. {
    4. assert(pos);
    5. ListNode* Prv = pos->prv;
    6. ListNode* tail = pos->next;
    7. Prv->next = tail;
    8. tail->prv = Prv;
    9. free(pos);
    10. }

    删除pos的下一个

    这里得注意的是又是是循环的。如果pos没有下一个就指向head,所以判断有没有pos的下一个必须有头~!!!

    1. //删除pos位置的下一个
    2. void ListEraseAfter(ListNode* head, ListNode* pos)
    3. {
    4. assert(pos);
    5. assert(pos->next != head);//没有下一个节点可删
    6. ListNode* tail = pos->next;
    7. pos->next = tail->next;
    8. tail->prv = pos;
    9. free(tail);
    10. }

    OK,测试一把~!

    1. void testErase()
    2. {
    3. ListNode* L = ListInit();
    4. ListPushBack(L, 1);
    5. ListPushBack(L, 2);
    6. ListPushBack(L, 3);
    7. ListPrint(L);
    8. ListErase(ListFind(L, 1));
    9. ListPrint(L);
    10. ListEraseAfter(L, ListFind(L, 2));
    11. ListPrint(L);
    12. ListDestory(L);
    13. L = NULL;
    14. }

    没有问题,上面有个修改函数忘了测试了,等在写一个长度一起在这个里面测试了:

    获取长度

    1. //获取链表的长度
    2. int ListSize(ListNode* head)
    3. {
    4. assert(head);
    5. int size = 0;
    6. ListNode* cur = head->next;
    7. while (cur != head)
    8. {
    9. ++size;
    10. cur = cur->next;
    11. }
    12. return size;
    13. }

    OK,测试一下:

    1. void testErase()
    2. {
    3. ListNode* L = ListInit();
    4. ListPushBack(L, 1);
    5. ListPushBack(L, 2);
    6. ListPushBack(L, 3);
    7. ListPrint(L);
    8. printf("size = %d\n", ListSize(L));
    9. ListErase(ListFind(L, 1));
    10. ListPrint(L);
    11. ListEraseAfter(L, ListFind(L, 2));
    12. ListPrint(L);
    13. printf("size = %d\n", ListSize(L));
    14. ListModif(L, 2, 200);
    15. ListPrint(L);
    16. ListDestory(L);
    17. L = NULL;
    18. }

    OK,这就是带头双向循环链表!链表的实现也就到此结束了,下面我们来对顺序表和链表做一个全面的对比!

    六、链表和顺序表的区别

    这里我们先来用单链表和顺序表对比,然后再用链表的最优结构---->带头双向循环链表来对比~!

    OK,我们再来对比一下顺序表和带头双向循环链表的区别~!

    这就是他们的区别,这里可以看出如果你需要大量查找和排序等操作时顺序表是优选,如果说是频繁的插入删除的话链表优选~!不能说他们谁好谁不好,知识应用的场景不同,他们其实是互补的数据结构~!OK,这里提到了一个东西叫缓存命中率~!这个是计算机存成原理里面的概念,这里实际上说的是存储体系和局部性原理~!OK我们大概谈谈:缓存利用率全称:CPU高速缓存利用率

    我们一般的计算机中的三大件是:cpu、内存、和本地磁盘~!我们写的数据结构的代码都在内存中。计组中有个存储器层次体系,就是把每个存储器按快慢给分层了,我们来看看:

    这里它实际上分为四层(按照价格以及快慢):

    寄存器和缓存实际上作用差不多,都是加载数据给cpu处理的,小的数据直接由寄存器来例如一个变量,大的数据由缓存去内存加载到cpu处理,例如数组!为什么CPU不直接到内存中去访问呢?原因很简单,CPU太快了内存和他不是一个等级,那内存以下的就更不要说了。

    为了解决这个问题电脑厂商会在CPU周围多弄几个寄存器和多级缓存;他们的速度是跟的上CPU的。这里你可能还会问既然有内存了为什么要磁盘呢?在这里有两方面的原因:一方面最直接的原因贵!另一方面是内存是带电存储而磁盘不是。内存直接能和上级交流磁盘则不行需要先加载到内存在于上级交流!

    另外还有一个是局部性原理,他分为局部性时间原理和局部性空间原理:下面是我找的一本计组上的解释:

    为什么说顺序表的缓存利用率高呢?根据局部性原理,CPU在访问万当前数据后它的下一个很有可能会被访问到,所以缓存会一次加载不止一个数据而是这个数据和后面的几个,具体几个得看硬件!这样cpu每次访问时缓存已经把顺序表的内容加载过去了,效率会高很多,原因是他们的地址是连续的!链表之所以低得原因是它的每个节点的内存地址大概率是不连续的,每一次访问一个节点的内容都要加载,而且他有可能会加载错造成缓存污染~!

    OK,好兄弟,链表就分享到这里!我们下期再见~~!

  • 相关阅读:
    聊聊流式数据湖Paimon(五)
    【毕业设计源码】PHP高校兼职跑腿系统
    【Spring Boot】如何自定义序列化以及反序列器
    使用位运算技巧比较两个数中较大的数
    Spring Boot 3.0正式发布及新特性解读
    使用 Istio CNI 支持强安全 TKE Stack 集群的服务网格流量捕获
    OpenCV入门9:图像增强和图像滤波
    zookeeper集群安装
    Oracle 快速入门
    Sentinel服务保护
  • 原文地址:https://blog.csdn.net/m0_75256358/article/details/133416026