当前位置:   article > 正文

九国服智多星带你手撕单链表(C语言实现)_c语言手撕链表

c语言手撕链表

前言:

在上一篇博客里,我们学习了顺序表这样的数据结构,我们发现顺序表虽然可以支持随机访问数据,但是在尾部之外的位置插入和删除数据需要挪动数据,效率很低。并且在顺序表扩容的时候难免会带来一些性能的消耗。为了解决顺序表的种种缺陷,链表这种数据结构就应运而生了!

目录

一.什么是链表

二.链表的结构

三.单链表的常见接口实现


一.什么是链表

链表是一种物理存储结构上非连续、非顺序的存储结构,而数据元素的逻辑顺序是通过链表中的指针实现的!如果用生活中的例子来举例的话,单链表的结构就和下面这种火车的结构是类似的

 我们可以形象的用一张图片来描述一个链表的结构:

 注意:在实际的物理结构中,并没有这样的箭头,能够找到下一个节点的原因是因为前一个节点存储了下一个节点的地址,从实际效果来看就好像有一个箭头指向了这个节点一样。

从这个案例我们就可以看出:

1.链式结构在逻辑上是连续的,在物理结构上并不一定是连续的。

2.链表的节点是通过动态内存函数malloc申请出来的

3.从堆上分配出来的空间,有自己的分配策略,可能在物理上连续,也可能不连续!

二.链表的结构

实际中,链表的类型有很多种,下面的情况组合起来就有8种情况:

1.单向或双向

 2.带头节点和不带头节点:

 3.循环或者非循环:

 这几种情况组合起来就有8种链表的结构,但是在实际工作中,最常用的链表的结构只有两种:

一:不带头单项不循环链表:

  无头单向不循环链表:结构简单,一般不会用来单独存储数据,更多的时候是作为高阶数据结构的子结构,比如哈希桶、以及图的邻接表等等,另外在笔试和OJ中也更多以这种结构为主。

 二:带头双向循环链表

带头双向循环链表:结构复杂,一般单独存储数据,实际中使用的数据结构也都是这种结构为主,虽然结构复杂,但在使用代码实现的时候,这个结构会有很大的优势,C++STL中的list也是基于这种结构实现的。

三.单链表的增删查改

和顺序表一样,SList.h--------->存放函数的声明和结构体的定义

SList.c----->实现各个函数接口 

Test.c---->测试函数功能逻辑:

SList.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. typedef int SLTDateType;
  6. typedef struct SListNode
  7. {
  8. SLTDateType data;
  9. struct SListNode* next;
  10. }SListNode;
  11. // 动态申请一个节点
  12. SListNode* BuySListNode(SLTDateType x);
  13. // 单链表打印
  14. void SListPrint(SListNode* plist);
  15. // 单链表尾插
  16. void SListPushBack(SListNode** pplist, SLTDateType x);
  17. // 单链表的头插
  18. void SListPushFront(SListNode** pplist, SLTDateType x);
  19. // 单链表的尾删
  20. void SListPopBack(SListNode** pplist);
  21. // 单链表头删
  22. void SListPopFront(SListNode** pplist);
  23. // 单链表查找
  24. SListNode* SListFind(SListNode* plist, SLTDateType x);
  25. // 单链表在pos位置之后插入x
  26. // 分析思考为什么不在pos位置之前插入?
  27. void SListInsertAfter(SListNode* pos, SLTDateType x);
  28. // 单链表删除pos位置之后的值
  29. // 分析思考为什么不删除pos位置?
  30. void SListEraseAfter(SListNode* pos);
  31. // 单链表的销毁
  32. void SListDestory(SListNode* plist);

我们按顺序进行对函数的说明:因为我们需要进行插入节点的动作,所以申请节点这件事情我们需要重复完成,因此我们把申请节点实现成一个接口以便于提高复用性:

  1. // 动态申请一个节点
  2. SListNode* BuySListNode(SLTDateType x)
  3. {
  4. SListNode* newnode=(SListNode*)malloc(sizeof(SListNode));
  5. if (NULL == newnode)
  6. {
  7. printf("malloc fail\n");
  8. exit(-1);
  9. }
  10. else
  11. {
  12. newnode->data = x;
  13. newnode->next = NULL;
  14. }
  15. return newnode;
  16. }

我们同样写一个Print函数来打印链表的元素:

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

接下来我们创建一个链表名为plist,开始的时候plist是NULL,接下来我们要尾插一些节点到这个链表并遍历打印

尾插的核心思想:使用一个尾指针tail记录尾节点,接下来将尾节点的下一个连接到新节点上,完成尾插,具体图解如下:

 开始的时候,链表为空,这时候直接把新节点的值赋给tail和newnode即可

 当tail不为空的时候,就把tail的下一个链接到新节点上:

 那么通过图片我们就可以写出如下代码:

  1. //这里形参是用phead,和plist和一样
  2. void SListNodePushBack(SListNode* phead, SLTDateType x)
  3. {
  4. SListNode* tail = phead;
  5. SListNode* newnode = BuySListNode(x);
  6. if (NULL == phead)
  7. {
  8. phead = tail = newnode;
  9. }
  10. else
  11. {
  12. tail->next = newnode;
  13. }
  14. }

接下来我们测试一下我们的代码逻辑:

  1. void TestSList()
  2. {
  3. SListNode* plist=NULL;
  4. SListPushBack(plist,1);
  5. SListPushBack(plist, 2);
  6. SListPrint(plist);
  7. }
  8. int main()
  9. {
  10. TestSList();
  11. return 0;
  12. }

程序运行结果如下:

 奇怪,我们明明插入了两个节点,为什么打印出来链表是空呢?当你不知道为什么程序出问题的时候,这时候调试是帮助你分析这个程序的最好办法。

调用完第二次尾插接口以后,调试信息反馈如下:

 我们发现,在调用两次尾插以后,plist并没有发生实际的改变!因为我们传递的是plist的拷贝,对拷贝的改变永远不会影响实际的plist,画个图帮助理解:

 所以我们在对phead的修改并不会影响plist,而我们第一次尾插会修改plist,所以为了修改plist,我们需要传plist的地址,所以改造代码如下:

  1. // 单链表尾插
  2. void SListPushBack(SListNode** pplist, SLTDateType x)
  3. {
  4. assert(pplist);
  5. SListNode* newnode = BuySListNode(x);
  6. //空链表的时候要单独处理
  7. if (NULL == *pplist)
  8. {
  9. *pplist = newnode;
  10. }
  11. else
  12. {
  13. SListNode* tail = *pplist;
  14. while (tail->next != NULL)
  15. {
  16. tail = tail->next;
  17. }
  18. tail->next = newnode;
  19. }
  20. }

接下来我们测试一下改造代码是否能够达到预期效果:

 我们看到,这份改造的代码确实达到了尾插的效果!

2.单链表头插:

相比于尾插需要找尾节点,头插相对于来说比较简单,我们也可以通过画图的方式帮助理解:

 实现代码如下:

  1. // 单链表的头插
  2. void SListPushFront(SListNode** pplist, SLTDateType x)
  3. {
  4. assert(pplist);
  5. SListNode* newnode = BuySListNode(x);
  6. //空链表要单独处理
  7. if (NULL == *pplist)
  8. {
  9. *pplist = newnode;
  10. }
  11. else
  12. {
  13. newnode->next = *pplist;
  14. *pplist = newnode;
  15. }
  16. }

运行测试结果如下:

 实现了头插和尾插,接下来我们接着实现尾删和头删。我们先来实现较为简单的头删。

头删就是从头部删除数据,但在删除节点之前我们要先保存下一个节点,否则我们一旦删除以后,如果没有保存下一节点的地址,整个链表就找不到了!,对于头删保存下一个节点,释放当前节点,把头节点更新成保存的下一个节点就可以了,所以说头删的复杂度是O(1)。

  1. // 单链表头删
  2. void SListPopFront(SListNode** pplist)
  3. {
  4. //pplist不能为空
  5. assert(pplist);
  6. //链表为空就不能删除
  7. assert(*pplist);
  8. //处理只有一个节点的情况
  9. if (NULL == (*pplist)->next)
  10. {
  11. free(*pplist);
  12. *pplist = NULL;
  13. }
  14. else
  15. {
  16. SListNode* next = (*pplist)->next;//->优先级高于*,注意运算逻辑
  17. free(*pplist);
  18. *pplist = next;
  19. }
  20. }

 经过我们前面的插入操作,现在链表存储的元素是-1->0->1->2

那么我们接下来调用一次头删,这个操作会把元素-1删除,最终打印出:0->1->2->NULL

调用完头删以后,程序打印结果如下:

 可以看出,我们写出的头删函数完美地完成了这一个任务。

接下来我们处理尾删的问题,和头删的方便快捷不同,尾删需要找尾节点,而由于我们这个链表是单项的,所以只能通过逐一遍历的方式才能找到尾节点,所以单链表的尾插时间复杂度是O(n)!

 我们需要两个指针:prev、tail

prev用来记录tail的前一个节点,tail用来寻找尾节点,找到尾节点后,由于prev已经保存了tail的前一个节点,所以我们直接释放tail。

 只有一个节点的时候,直接释放plist,并且把plist置空就可以了,根据图我们就可以写出代码:

  1. // 单链表的尾删
  2. void SListPopBack(SListNode** pplist)
  3. {//pplist不能为空
  4. assert(pplist);
  5. //链表为空就不能删除
  6. assert(*pplist);
  7. //处理只有一个节点的情况
  8. if (NULL == (*pplist)->next)
  9. {
  10. free(*pplist);
  11. *pplist = NULL;
  12. }
  13. //非空且节点数多于一个的情况
  14. else
  15. {
  16. SListNode* prev = NULL;
  17. SListNode* tail = *pplist;
  18. while (tail->next != NULL)
  19. {
  20. prev = tail;
  21. tail = tail->next;
  22. }
  23. free(tail);
  24. tail = prev;
  25. prev->next = NULL;
  26. }
  27. }

接下来我们尾删一个节点,尾删完成后,链表的元素应该是0->1,运行程序观察结果:

 程序和我们预期的结果一致,说明我们尾插的函数逻辑没有出错!

接下来我们就要实现InsertAfter方法和EraseAfter函数,这两个函数的功能是删除指定位置的下一个位置指向的节点,为什么是下一个节点而不是当前节点?理由和尾删一样,单链表只能找到后继,如果删除当前指向的节点,还是需要遍历链表寻找前驱节点,时间复杂度还是O(n),没有达到快速插入和删除的目的!

在完成Insert函数和Erase函数实现之前,我们可以先写一个Find函数来查找节点:

  1. SListNode* SListFind(SListNode* plist, SLTDateType x)
  2. {
  3. SListNode* cur = plist;
  4. while (cur)
  5. {
  6. if (x == cur->data)
  7. {
  8. return cur;
  9. }
  10. cur = cur->next;
  11. }
  12. return NULL;
  13. }

Insert函数的图解如下:

根据图片写出代码:

  1. void SListInsertAfter(SListNode* pos, SLTDateType x)
  2. {
  3. assert(pos);
  4. //只使用pos和newnode指针要注意连接顺序!
  5. SListNode* newnode = BuySListNode(x);
  6. newnode->next = pos->next;
  7. pos->next = newnode;
  8. }

 画出图来分析Erase函数:

Erase的代码如下:

  1. oid SListEraseAfter(SListNode* pos)
  2. {
  3. assert(pos);
  4. assert(pos->next);
  5. //保存pos->next的下一个节点
  6. SListNode* Next = pos->next->next;
  7. free(pos->next);
  8. pos->next = Next;
  9. }

我们接下来来测试代码:

  1. void TestSList()
  2. {
  3. SListNode* plist = NULL;
  4. SListPushBack(&plist, 0);
  5. SListPushBack(&plist, 1);
  6. SListPushBack(&plist, 2);
  7. SListPushBack(&plist, 3);
  8. SListPushBack(&plist, 4);
  9. SListNode* pos1 = SListFind(plist, 2);
  10. SListNode* pos2 = SListFind(plist, 3);
  11. if (pos1)
  12. {
  13. printf("插入值是5的新节点\n");
  14. SListInsertAfter(pos1, 5);
  15. SListPrint(plist);
  16. }
  17. if (pos2)
  18. {
  19. printf("删除值是3的节点的下一个节点\n");
  20. SListEraseAfter(pos2);
  21. SListPrint(plist);
  22. }
  23. }

 可以看到,我们写的Insert和Erase代码完美达到了预期的要求!

最后,因为节点是动态申请的,所以我们需要写一个释放的函数:

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

完整代码如下:

SList.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. typedef int SLTDateType;
  6. typedef struct SListNode
  7. {
  8. SLTDateType data;
  9. struct SListNode* next;
  10. }SListNode;
  11. // 动态申请一个节点
  12. SListNode* BuySListNode(SLTDateType x);
  13. // 单链表打印
  14. void SListPrint(SListNode* plist);
  15. // 单链表尾插
  16. void SListPushBack(SListNode** pplist, SLTDateType x);
  17. // 单链表的头插
  18. void SListPushFront(SListNode** pplist, SLTDateType x);
  19. // 单链表的尾删
  20. void SListPopBack(SListNode** pplist);
  21. // 单链表头删
  22. void SListPopFront(SListNode** pplist);
  23. // 单链表查找
  24. SListNode* SListFind(SListNode* plist, SLTDateType x);
  25. // 单链表在pos位置之后插入x
  26. // 分析思考为什么不在pos位置之前插入?
  27. void SListInsertAfter(SListNode* pos, SLTDateType x);
  28. // 单链表删除pos位置之后的值
  29. // 分析思考为什么不删除pos位置?
  30. void SListEraseAfter(SListNode* pos);
  31. // 单链表的销毁
  32. void SListDestory(SListNode* plist);

SList.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "SList.h"
  3. // 动态申请一个节点
  4. SListNode* BuySListNode(SLTDateType x)
  5. {
  6. SListNode* newnode=(SListNode*)malloc(sizeof(SListNode));
  7. if (NULL == newnode)
  8. {
  9. printf("malloc fail\n");
  10. exit(-1);
  11. }
  12. else
  13. {
  14. newnode->data = x;
  15. newnode->next = NULL;
  16. }
  17. return newnode;
  18. }
  19. // 单链表打印
  20. void SListPrint(SListNode* plist)
  21. {
  22. SListNode* cur = plist;
  23. while (cur)
  24. {
  25. printf("%d->", cur->data);
  26. cur = cur->next;
  27. }
  28. printf("NULL\n");
  29. }
  30. // 单链表尾插
  31. void SListPushBack(SListNode** pplist, SLTDateType x)
  32. {
  33. assert(pplist);
  34. SListNode* newnode = BuySListNode(x);
  35. //空链表的时候要单独处理
  36. if (NULL == *pplist)
  37. {
  38. *pplist = newnode;
  39. }
  40. else
  41. {
  42. SListNode* tail = *pplist;
  43. while (tail->next != NULL)
  44. {
  45. tail = tail->next;
  46. }
  47. tail->next = newnode;
  48. }
  49. }
  50. // 单链表的头插
  51. void SListPushFront(SListNode** pplist, SLTDateType x)
  52. {
  53. assert(pplist);
  54. SListNode* newnode = BuySListNode(x);
  55. //空链表要单独处理
  56. if (NULL == *pplist)
  57. {
  58. *pplist = newnode;
  59. }
  60. else
  61. {
  62. newnode->next = *pplist;
  63. *pplist = newnode;
  64. }
  65. }
  66. // 单链表的尾删
  67. void SListPopBack(SListNode** pplist)
  68. {//pplist不能为空
  69. assert(pplist);
  70. //链表为空就不能删除
  71. assert(*pplist);
  72. //处理只有一个节点的情况
  73. if (NULL == (*pplist)->next)
  74. {
  75. free(*pplist);
  76. *pplist = NULL;
  77. }
  78. //非空且节点数多于一个的情况
  79. else
  80. {
  81. SListNode* prev = NULL;
  82. SListNode* tail = *pplist;
  83. while (tail->next != NULL)
  84. {
  85. prev = tail;
  86. tail = tail->next;
  87. }
  88. free(tail);
  89. tail = prev;
  90. prev->next = NULL;
  91. }
  92. }
  93. // 单链表头删
  94. void SListPopFront(SListNode** pplist)
  95. {
  96. //pplist不能为空
  97. assert(pplist);
  98. //链表为空就不能删除
  99. assert(*pplist);
  100. //处理只有一个节点的情况
  101. if (NULL == (*pplist)->next)
  102. {
  103. free(*pplist);
  104. *pplist = NULL;
  105. }
  106. else
  107. {
  108. SListNode* next = (*pplist)->next;
  109. free(*pplist);
  110. *pplist = next;
  111. }
  112. }
  113. // 单链表查找
  114. SListNode* SListFind(SListNode* plist, SLTDateType x)
  115. {
  116. SListNode* cur = plist;
  117. while (cur)
  118. {
  119. if (x == cur->data)
  120. {
  121. return cur;
  122. }
  123. cur = cur->next;
  124. }
  125. return NULL;
  126. }
  127. // 单链表在pos位置之后插入x
  128. // 分析思考为什么不在pos位置之前插入?---->需要遍历找到前驱,时间复杂度是o(n)
  129. void SListInsertAfter(SListNode* pos, SLTDateType x)
  130. { //保存原有的节点
  131. SListNode* newnode = BuySListNode(x);
  132. newnode->next = pos->next;
  133. pos->next = newnode;
  134. }
  135. // 单链表删除pos位置之后的值
  136. // 分析思考为什么不删除pos位置?---->需要遍历找到前驱,时间复杂度是o(n)
  137. void SListEraseAfter(SListNode* pos)
  138. {
  139. assert(pos);
  140. assert(pos->next);
  141. SListNode* Next = pos->next->next;
  142. free(pos->next);
  143. pos->next = Next;
  144. }
  145. // 单链表的销毁
  146. void SListDestory(SListNode* plist)
  147. {
  148. SListNode* cur = plist;
  149. SListNode* Next = NULL;
  150. while (cur)
  151. {
  152. Next = cur->next;
  153. free(cur);
  154. cur = Next;
  155. }
  156. }

test.c:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "SList.h"
  3. void TestSList()
  4. {
  5. SListNode* plist = NULL;
  6. SListPushBack(&plist, 0);
  7. SListPushBack(&plist, 1);
  8. SListPushBack(&plist, 2);
  9. SListPushBack(&plist, 3);
  10. SListPushBack(&plist, 4);
  11. SListNode* pos1 = SListFind(plist, 2);
  12. SListNode* pos2 = SListFind(plist, 3);
  13. if (pos1)
  14. {
  15. printf("插入值是5的新节点\n");
  16. SListInsertAfter(pos1, 5);
  17. SListPrint(plist);
  18. }
  19. if (pos2)
  20. {
  21. printf("删除值是3的节点的下一个节点\n");
  22. SListEraseAfter(pos2);
  23. SListPrint(plist);
  24. }
  25. SListDestory(plist);
  26. }
  27. int main()
  28. {
  29. TestSList();
  30. return 0;
  31. }

下期预告:

本篇文章实现了单链表的增删查改,这是单链表的基础,下一篇博客将会介绍中常见的关于链表的OJ题和曾经作为面经的链表成环追逐问题,敬请期待!!!

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

闽ICP备14008679号