深圳幻海软件技术有限公司 欢迎您!

单链表(数据结构)(C语言)

2023-04-25

这里特指无哨兵位单向非循环链表目录背景概念单链表的实现前景提示单链表的结构体定义单链表的打印关于为什么不断言phead关于单链表的逻辑结构和物理结构单链表的尾插关于为什么要用到二级指针关于尾插的本质关于找尾整个过程的解释关于为什么打印单链表就不需要传二级指针单链表的动态申请结点单链表的头插单链表的尾

这里特指无哨兵位单向非循环链表

目录

背景

概念

单链表的实现

前景提示

单链表的结构体定义

单链表的打印

关于为什么不断言phead

关于单链表的逻辑结构和物理结构

单链表的尾插

关于为什么要用到二级指针

关于尾插的本质

关于找尾整个过程的解释

关于为什么打印单链表就不需要传二级指针

单链表的动态申请结点

单链表的头插

单链表的尾删

单链表的头删

链表的查找

单链表在pos位置之前插入x(也可以理解为在pos位置插入)

单链表删除pos位置之前的值(也可以理解为删除pos位置的值)

单链表在pos位置之后插入x 

单链表删除pos位置之后的值

关于不传头指针如何在pos前插入/删除(巧思)

单链表的销毁

总代码(想直接看结果的可以看这里)


背景

上一篇文章我们学习了顺序表。

但顺序表要求的是连续的物理空间,这就导致了其有以下几个缺点:

1. 中间/头部的插入删除,时间复杂度为O(N)。
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
为了更好地解决以上问题,我们就引申出了链表。

概念

链表是一种 物理存储结构上非连续、非顺序的存储结构,数据元素的 逻辑顺序是通过链表 中的 指针链接次序实现的 。链表中一个数据存一个内存块。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的 数据域,另一个是存储下一个结点地址的 指针域
单链表是一种链式存取的数据结构,用一组 地址任意的存储单元(这组存储单元既可以是连续的,也可以是不连续的)存放线性表中的数据元素。链表中的数据是以 结点( 每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。)来表示的,每个结点的构成: 元素+指针,元素就是存储数据的 存储单元,指针就是连接每个结点的 地址数据
单链表中每个结点的存储地址是存放在其前趋结点 next 域中,而开始结点无前趋,故设头指针head指向开始结点。链表由头指针 唯一确定,单链表可以用头指针的名字来命名,终端结点无后继,故终端结点的指针域为空,即NULL。

单链表的实现

前景提示

SList.h  用于  引用的头文件、单链表的定义、函数的声明。

SList.c  用于  函数的定义。

Test.c    用于  链表功能的测试。

单链表的结构体定义

在SList.h下

  1. #pragma once//使同一个文件不会被包含(include)多次,不必担心宏名冲突
  2. //先将可能使用到的头文件写上
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<assert.h>
  6. typedef int SLTDataType;//假设结点的数据域类型为 int
  7. //单链表的结构体定义
  8. typedef struct SListNode
  9. {
  10. SLTDataType data;//结点的数据域,用来存储数据元素
  11. struct SListNode* next;//结点的指针域,用来存储下一个结点地址的指针域
  12. //next的意思就是下一个结点的指针,上一个结点存的是下一个结点的地址
  13. //每个结点都是结构体指针类型
  14. //有些人会把上一行代码写成SListNode* next;这是不对的,因为C语言中
  15. //struct SListNode 整体才是一个类型(但C++可以)
  16. //或者写成SLTNode* next;这也是错的,因为编译器的查找规则是从上忘下找
  17. }SLTNode;

单链表的打印

要理解单链表,首先我们先写一个单链表的打印。

在SList.h下

  1. //链表的打印——助于理解链表
  2. void SLTPrint(SLTNode* phead);
在SList.c下
  1. #include "SList.h"//别忘了
  2. //链表的打印
  3. void SLTPrint(SLTNode* phead)
  4. {
  5. //assert(phead);这里并不需要断言phead不为空
  6. //为什么这里不需要断言phead?
  7. //空链表可以打印,即phead==NULL可以打印,直接断言就不合适了
  8. //那空顺序表也可以打印,那它为什么就要断言呢?
  9. //因为phead是指向第一个存有数据的结点的
  10. //而顺序表的ps是指向一个结构体
  11. SLTNode* cur = phead;//将phead赋值给cur,所以cur也指向第一个结点
  12. while (cur != NULL)//或while(cur)
  13. {
  14. printf("%d->", cur->data);//打印的时候加了个箭头更方便理解
  15. cur = cur->next;//next是下一个结点的地址
  16. //++cur/cur++;这种是不行的,指针加加,加到的是连续的下一个位置
  17. //链表的每一个结点都是单独malloc出来的,我们不能保证结点之间的地址是连续的
  18. }
  19. printf("NULL\n");
  20. }

关于为什么不断言phead

关于单链表的逻辑结构和物理结构

                     

                     

                   

                     

在打印之前,我们得先有数据

单链表的尾插

在SList.h下

  1. // 单链表尾插
  2. void SLTPushBack(SLTNode** pphead, SLTDataType x);
  3. //用二级指针,解释看下文,x为要插入的数据

关于为什么要用到二级指针

在SList.c下

  1. // 单链表尾插
  2. void SLTPushBack(SLTNode** pphead, SLTDataType x)
  3. {
  4. assert(pphead);//pphead是plist的地址,不能为空
  5. //注意区分几个断言的判断,plist有可能是空,pphead一定不能为空
  6. SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));//先创建个结点
  7. if (newnode == NULL)//如果malloc失败
  8. {
  9. perror("malloc fail");
  10. return;
  11. }
  12. //如果malloc成功
  13. newnode->data = x;//插入的数据
  14. newnode->next = NULL;//初始化为空
  15. //找尾(尾插之前先找到尾)
  16. if (*pphead == NULL)//若链表为空
  17. {
  18. *pphead = newnode;
  19. }
  20. else//若链表不为空
  21. {
  22. SLTNode* tail = *pphead;
  23. while (tail->next != NULL)//对于不为空的链表:尾插的本质
  24. //是原尾结点要存新尾结点的地址
  25. {
  26. tail = tail->next;
  27. }
  28. tail->next = newnode;
  29. /*有些同学会写成:
  30. while (tail != NULL)
  31. {
  32. tail = tail->next;
  33. }
  34. tail = newnode;*/
  35. }
  36. }

关于尾插的本质

 

 而

关于找尾整个过程的解释

 

 ↓

在Test.c下

  1. #include "SList.h"//别忘了
  2. //用于函数功能的测试
  3. void TestSList1()
  4. {
  5. SLTNode* plist = NULL;
  6. SLTPushBack(&plist, 1);
  7. SLTPushBack(&plist, 2);
  8. SLTPushBack(&plist, 3);
  9. SLTPushBack(&plist, 4);
  10. SLTPrint(plist);
  11. }
  12. int main()
  13. {
  14. TestSList1();
  15. return 0;
  16. }

关于为什么打印单链表就不需要传二级指针

因为打印单链表没有改变指针。如果要改变传过去的指针(实参),那就要传实参的地址,不改变就不传。

单链表的动态申请结点

要写头插时,我们发现不管是尾插和头插都要动态申请一个结点,所以我们可以先写一个函数来复用。

在SList.c下

  1. // 动态申请一个结点
  2. SLTNode* BuySLTNode(SLTDataType x)
  3. {
  4. //同样不需要断言
  5. SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));//先创建个结点
  6. if (newnode == NULL)//如果malloc失败
  7. {
  8. perror("malloc fail");
  9. return NULL;
  10. }
  11. //如果malloc成功
  12. newnode->data = x;//插入的数据
  13. newnode->next = NULL;//初始化为空
  14. return newnode;//返回newnode
  15. }
  16. // 单链表尾插
  17. void SLTPushBack(SLTNode** pphead, SLTDataType x)
  18. {
  19. assert(pphead);//pphead是plist的地址,不能为空
  20. SLTNode* newnode = BuySLTNode(x);
  21. //找尾(尾插之前先找到尾)
  22. if (*pphead == NULL)//若链表为空
  23. {
  24. *pphead = newnode;
  25. }
  26. else//若链表不为空
  27. {
  28. SLTNode* tail = *pphead;
  29. while (tail->next != NULL)
  30. //对于不为空的链表:尾插的本质是原尾结点要存新尾结点的地址
  31. {
  32. tail = tail->next;
  33. }
  34. tail->next = newnode;
  35. /*有些同学会写成:
  36. while (tail != NULL)
  37. {
  38. tail = tail->next;
  39. }
  40. tail = newnode;*/
  41. }
  42. }

单链表的头插

 在SList.h下

  1. // 单链表的头插
  2. void SLTPushFront(SLTNode** pphead, SLTDataType x);

在SList.c下

  1. // 单链表的头插
  2. void SLTPushFront(SLTNode** pphead, SLTDataType x)
  3. {
  4. //发现plist不管是否为空,头插的方法都一样
  5. SLTNode* newnode = BuySLTNode(x);
  6. newnode->next = *pphead;
  7. *pphead = newnode;
  8. }

在Test.c下

  1. void TestSList1()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. }
  10. void TestSList2()
  11. {
  12. SLTNode* plist = NULL;
  13. SLTPushFront(&plist, 1);
  14. SLTPushFront(&plist, 2);
  15. SLTPushFront(&plist, 3);
  16. SLTPushFront(&plist, 4);
  17. SLTPrint(plist);
  18. }
  19. int main()
  20. {
  21. TestSList1();
  22. TestSList2();
  23. return 0;
  24. }

单链表的尾删

尾删是否需要二级指针?要!

在SList.h下

  1. // 单链表的尾删
  2. void SLTPopBack(SLTNode** pphead);

在Test.c下

  1. void TestSList2()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushFront(&plist, 1);
  5. SLTPushFront(&plist, 2);
  6. SLTPushFront(&plist, 3);
  7. SLTPushFront(&plist, 4);
  8. SLTPrint(plist);
  9. SLTPopBack(&plist);
  10. SLTPrint(plist);
  11. }
  12. int main()
  13. {
  14. TestSList2();
  15. return 0;
  16. }

在SList.c下

有些人一开始会这样写:

  1. // 单链表的尾删
  2. void SLTPopBack(SLTNode** pphead)
  3. {
  4. assert(pphead);//pphead是plist的地址,不能为空
  5. SLTNode* tail = *pphead;
  6. while (tail->next != NULL)
  7. {
  8. tail = tail->next;
  9. }
  10. free(tail);
  11. tail = NULL;
  12. }

结果是:

出现随机值——>很有可能是因为野指针。

 

 为什么呢?

这里给更改后的SList.c的两种方法

法一:

  1. // 单链表的尾删
  2. void SLTPopBack(SLTNode** pphead)
  3. {
  4. assert(pphead);//pphead是plist的地址,不能为空
  5. //法一:
  6. SLTNode* prev=NULL;
  7. SLTNode* tail = *pphead;
  8. while (tail->next != NULL)
  9. {
  10. prev = tail;
  11. tail = tail->next;
  12. }
  13. free(tail);
  14. tail = NULL;
  15. prev->next = NULL;
  16. }

法二:

  1. // 单链表的尾删
  2. void SLTPopBack(SLTNode** pphead)
  3. {
  4. assert(pphead);//pphead是plist的地址,不能为空
  5. //法二:
  6. SLTNode* tail = *pphead;
  7. while (tail->next->next != NULL)
  8. {
  9. tail = tail->next;
  10. }
  11. free(tail->next);
  12. tail->next = NULL;
  13. }

 但是我们再多测试几组

在Test.c下

  1. void TestSList2()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushFront(&plist, 1);
  5. SLTPushFront(&plist, 2);
  6. SLTPushFront(&plist, 3);
  7. SLTPushFront(&plist, 4);
  8. SLTPrint(plist);
  9. //尾删四个数据
  10. SLTPopBack(&plist);
  11. SLTPrint(plist);
  12. SLTPopBack(&plist);
  13. SLTPrint(plist);
  14. SLTPopBack(&plist);
  15. SLTPrint(plist);
  16. SLTPopBack(&plist);
  17. SLTPrint(plist);
  18. }
  19. int main()
  20. {
  21. TestSList2();
  22. return 0;
  23. }

结果:

两方法最后都还剩一个!

原因是未考虑到只有一个结点或没有结点的情况。

这里是再次更改后的SList.c

  1. // 单链表的尾删
  2. void SLTPopBack(SLTNode** pphead)
  3. {
  4. assert(pphead);//pphead是plist的地址,不能为空
  5. //检查有无结点
  6. assert(*pphead != NULL);
  7. //1.只有一个结点
  8. if ((*pphead)->next == NULL)
  9. {
  10. free(*pphead);
  11. *pphead = NULL;
  12. }
  13. else
  14. {
  15. //2.有多个结点
  16. /*//法一:
  17. SLTNode* prev=NULL;
  18. SLTNode* tail = *pphead;
  19. while (tail->next != NULL)
  20. {
  21. prev = tail;
  22. tail = tail->next;
  23. }
  24. free(tail);
  25. tail = NULL;
  26. prev->next = NULL;*/
  27. //法二:
  28. SLTNode* tail = *pphead;
  29. while (tail->next->next != NULL)
  30. {
  31. tail = tail->next;
  32. }
  33. free(tail->next);
  34. tail->next = NULL;
  35. }
  36. }

只有一个结点

没有结点 

单链表的头删

在SList.h下

  1. // 单链表头删
  2. void SLTPopFront(SLTNode** pphead);

在SList.c下

  1. // 单链表头删
  2. void SLTPopFront(SLTNode** pphead)
  3. {
  4. //检查有无结点
  5. assert(*pphead != NULL);
  6. SLTNode* first = *pphead;
  7. *pphead = first->next;
  8. free(first);
  9. first = NULL;
  10. }

在Test.c下

  1. TestSList3()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushFront(&plist, 1);
  5. SLTPushFront(&plist, 2);
  6. SLTPushFront(&plist, 3);
  7. SLTPushFront(&plist, 4);
  8. SLTPrint(plist);
  9. SLTPopFront(&plist);
  10. SLTPrint(plist);
  11. SLTPopFront(&plist);
  12. SLTPrint(plist);
  13. SLTPopFront(&plist);
  14. SLTPrint(plist);
  15. SLTPopFront(&plist);
  16. SLTPrint(plist);
  17. }
  18. int main()
  19. {
  20. TestSList3();
  21. return 0;
  22. }

链表的查找

在SList.h下

  1. // 单链表查找
  2. SLTNode* SLTFind(SLTNode* phead, SLTDataType x);

在SList.c下

  1. // 单链表查找
  2. SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
  3. {
  4. SLTNode* cur = phead;//用cur去遍历,不用phead
  5. while (cur)//找x
  6. {
  7. if (cur->data == x)//如果找到了
  8. {
  9. return cur;
  10. }
  11. cur = cur->next;
  12. }
  13. return NULL;//如果找不到
  14. }

在Test.c下

  1. void TestSList4()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. //将寻找和修改结合
  10. //eg:值为2的结点*2
  11. SLTNode* ret = SLTFind(plist, 2);
  12. ret->data *= 2;
  13. SLTPrint(plist);
  14. }
  15. int main()
  16. {
  17. TestSList4();
  18. return 0;
  19. }

单链表在pos位置之前插入x(也可以理解为在pos位置插入)

在SList.h下

  1. //单链表在pos位置之前插入x(效率较低)(得传头指针)(也可以理解为在pos位置插入)
  2. void SLTInsertBefore(SLTNode** pphead, SLTNode* pos, SLTDataType x);

在SList.c下

  1. //单链表在pos位置之前插入x(也可以理解为在pos位置插入)
  2. void SLTInsertBefore(SLTNode** pphead, SLTNode* pos, SLTDataType x)
  3. {
  4. assert(pphead);//pphead是plist的地址,不能为空
  5. assert(pos);//默认pos一定会找到
  6. if (pos == *pphead)//如果pos在第一个位置——那就是头插
  7. {
  8. SLTPushFront(pphead, x);
  9. }
  10. else//如果pos不是第一个位置
  11. {
  12. //找到pos的前一个位置
  13. SLTNode* prev = *pphead;
  14. while (prev->next != pos)
  15. {
  16. prev = prev->next;
  17. }
  18. SLTNode* newnode = BuySLTNode(x);
  19. prev->next = newnode;
  20. newnode->next = pos;
  21. }
  22. }

在Test.c下

  1. TestSList5()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. //寻找值为2的结点
  10. SLTNode* ret = SLTFind(plist, 2);
  11. SLTInsertBefore(&plist, ret, 20);//在该结点前插入值为20的结点
  12. SLTPrint(plist);
  13. }
  14. int main()
  15. {
  16. TestSList5();
  17. return 0;
  18. }

单链表删除pos位置之前的值(也可以理解为删除pos位置的值)

在SList.h下

  1. // 单链表删除pos位置之前的值(效率较低)(得传头指针)(也可以理解为删除pos位置的值)
  2. void SLTEraseBefore(SLTNode** pphead, SLTNode* pos);

在SList.c下

  1. // 单链表删除pos位置之前的值(也可以理解为删除pos位置的值)
  2. void SLTEraseBefore(SLTNode** pphead, SLTNode* pos)
  3. {
  4. assert(pphead);
  5. assert(pos);
  6. if (*pphead == pos)//如果pos在第一个位置
  7. {
  8. SLTPopFront(pphead);//头删
  9. }
  10. else//如果不在第一个位置
  11. {
  12. SLTNode* prev = *pphead;
  13. while (prev->next != pos)
  14. {
  15. prev = prev->next;
  16. }
  17. prev->next = pos->next;
  18. free(pos);
  19. //pos = NULL;形参的改变不影响实参,加不加这句话都可以
  20. }
  21. }

在Test.c下

  1. TestSList6()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. //寻找值为2的结点
  10. SLTNode* ret = SLTFind(plist, 2);
  11. SLTEraseBefore(&plist, ret);
  12. ret = NULL;//一般在这里置空
  13. SLTPrint(plist);
  14. }
  15. int main()
  16. {
  17. TestSList6();
  18. return 0;
  19. }

单链表在pos位置之后插入x 

在SList.h下

  1. // 单链表在pos位置之后插入x,单链表比较适合这种
  2. void SLTInsertAfter(SLTNode* pos, SLTDataType x);

在SList.c下

有些人会这样写:

  1. //单链表在pos位置之后插入x
  2. void SLTInsertAfter(SLTNode* pos, SLTDataType x)
  3. {
  4. assert(pos);
  5. SLTNode* newnode = BuySLTNode(x);
  6. pos->next = newnode;
  7. newnode->next=pos->next;
  8. }

后果:

 所以橙色和紫色的两步应该互换位置

更改后的SList.c

  1. //单链表在pos位置之后插入x
  2. void SLTInsertAfter(SLTNode* pos, SLTDataType x)
  3. {
  4. assert(pos);
  5. SLTNode* newnode = BuySLTNode(x);
  6. newnode->next = pos->next;
  7. pos->next = newnode;
  8. }

在Test.c下

  1. TestSList7()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. SLTNode* ret = SLTFind(plist, 2);
  10. SLTInsertAfter(ret, 20);
  11. SLTPrint(plist);
  12. }
  13. int main()
  14. {
  15. TestSList7();
  16. return 0;
  17. }

单链表删除pos位置之后的值

在SList.h下

  1. // 单链表删除pos位置之后的值,单链表比较适合这种
  2. void SLTEraseAfter(SLTNode* pos);

在SList.c下

  1. // 单链表删除pos位置之后的值
  2. void SLTEraseAfter(SLTNode* pos)
  3. {
  4. assert(pos);
  5. assert(pos->next);
  6. SLTNode* del = pos->next;//保存要删除的结点
  7. pos->next = pos->next->next;//或者写成pos->next=del->next;
  8. free(del);
  9. del = NULL;
  10. }

在Test.c下

  1. TestSList8()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. SLTNode* ret = SLTFind(plist, 2);
  10. SLTEraseAfter(ret);
  11. SLTPrint(plist);
  12. }
  13. int main()
  14. {
  15. TestSList8();
  16. return 0;
  17. }

关于不传头指针如何在pos前插入/删除(巧思)

插入:先利用单链表在pos位置之后插入x的函数,再交换pos和pos->next的值。

在SList.h下

  1. // 不传头指针,在pos前插入x(也可以理解为在pos位置插入)
  2. void SLTInsertBefore1(SLTNode* pos, SLTDataType x);

在SList.c下

  1. // 不传头指针,在pos前插入x(也可以理解为在pos位置插入)
  2. void SLTInsertBefore1(SLTNode* pos, SLTDataType x)
  3. {
  4. assert(pos);
  5. //调用单链表在pos位置之后插入x的函数
  6. SLTInsertAfter(pos, x);
  7. //交换pos和pos->next的值
  8. SLTDataType temp;
  9. temp = pos->data;
  10. pos->data = pos->next->data;
  11. pos->next->data = temp;
  12. }

在Test.c下

  1. TestSList9()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. SLTNode* ret = SLTFind(plist, 2);
  10. SLTInsertBefore1(ret,20);
  11. SLTPrint(plist);
  12. }
  13. int main()
  14. {
  15. TestSList9();
  16. return 0;
  17. }

删除:先将pos->next的值赋给pos,再利用单链表删除pos位置之后的值的函数。(但此方法不能尾删)

在SList.h下

  1. // 不传头指针,删除pos位置之前的值(也可以理解为删除pos位置的值)
  2. void SLTEraseBefore1(SLTNode* pos);

在SList.c下

  1. // 不传头指针,删除pos位置之前的值(也可以理解为删除pos位置的值)
  2. void SLTEraseBefore1(SLTNode* pos)
  3. {
  4. assert(pos);
  5. assert(pos->next);//不能尾删
  6. SLTNode* del = pos->next;
  7. pos->data = pos->next->data;
  8. pos->next = pos->next->next;
  9. free(del);
  10. del = NULL;
  11. }

在Test.c下

  1. TestSList10()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. SLTNode* ret = SLTFind(plist, 2);
  10. SLTEraseBefore1(ret);
  11. SLTPrint(plist);
  12. }
  13. int main()
  14. {
  15. TestSList10();
  16. return 0;
  17. }

单链表的销毁

方法一(不传二级指针):

在SList.h下

  1. // 单链表的销毁,不传二级
  2. void SLTDestroy(SLTNode* phead);

在SList.c下

  1. // 单链表的销毁
  2. void SLTDestroy(SLTNode* phead)
  3. {
  4. SLTNode* cur = phead;
  5. /*//有些人一开始会这样写
  6. while (cur)
  7. {
  8. //free不是销毁这个指针指向的内存,而是将指针指向的内存还给操作系统
  9. free(cur);//cur依旧指向free之前的地址
  10. cur = cur->next;
  11. }*/
  12. while (cur)
  13. {
  14. SLTNode* tmp = cur->next;
  15. free(cur);
  16. cur = tmp;
  17. }
  18. }

在Test.c下

  1. TestSList11()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. SLTDestroy(plist);
  10. plist = NULL;
  11. }
  12. int main()
  13. {
  14. TestSList11();
  15. return 0;
  16. }

方法二(传二级指针):

在SList.h下

  1. // 单链表的销毁,传二级
  2. void SLTDestroy1(SLTNode** pphead);

在SList.c下

  1. // 单链表的销毁,传二级
  2. void SLTDestroy1(SLTNode** pphead)
  3. {
  4. assert(pphead);
  5. SLTNode* cur = *pphead;
  6. while (cur)
  7. {
  8. SLTNode* tmp = cur->next;
  9. free(cur);
  10. cur = tmp;
  11. }
  12. *pphead = NULL;
  13. }

在Test.c下

  1. TestSList12()
  2. {
  3. SLTNode* plist = NULL;
  4. SLTPushBack(&plist, 1);
  5. SLTPushBack(&plist, 2);
  6. SLTPushBack(&plist, 3);
  7. SLTPushBack(&plist, 4);
  8. SLTPrint(plist);
  9. SLTDestroy1(&plist);
  10. SLTPrint(plist);
  11. }
  12. int main()
  13. {
  14. TestSList12();
  15. return 0;
  16. }

总代码(想直接看结果的可以看这里)

在SList.h下

  1. #pragma once//使同一个文件不会被包含(include)多次,不必担心宏名冲突
  2. //先将可能使用到的头文件写上
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<assert.h>
  6. typedef int SLTDataType;//假设结点的数据域类型为 int
  7. // 单链表的结构体定义
  8. //↓结点 单链表 Singly Linked List
  9. typedef struct SListNode
  10. {
  11. SLTDataType data;//结点的数据域,用来存储数据元素
  12. struct SListNode* next;//结点的指针域,用来存储下一个结点地址的指针域
  13. //next的意思就是下一个结点的指针,上一个结点存的是下一个结点的地址
  14. //每个结点都是结构体指针类型
  15. //有些人会把上一行代码写成SListNode* next;
  16. //这是不对的,因为C语言中 struct SListNode 整体才是一个类型(但C++可以)
  17. //或者写成SLTNode* next;这也是不对的,因为编译器的查找规则是从上忘下找
  18. }SLTNode;
  19. // 链表的打印——助于理解链表
  20. void SLTPrint(SLTNode* phead);
  21. // 单链表尾插
  22. void SLTPushBack(SLTNode** pphead, SLTDataType x);//用二级指针,x为要插入的数据
  23. // 单链表的头插
  24. void SLTPushFront(SLTNode** pphead, SLTDataType x);
  25. // 单链表的尾删
  26. void SLTPopBack(SLTNode** pphead);
  27. // 单链表头删
  28. void SLTPopFront(SLTNode** pphead);
  29. // 单链表查找
  30. SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
  31. // 单链表在pos位置之前插入x(效率较低)(得传头指针)(也可以理解为在pos位置插入)
  32. void SLTInsertBefore(SLTNode** pphead, SLTNode* pos, SLTDataType x);
  33. // 单链表删除pos位置之前的值(效率较低)(得传头指针)(也可以理解为删除pos位置的值)
  34. void SLTEraseBefore(SLTNode** pphead, SLTNode* pos);
  35. // 单链表在pos位置之后插入x,单链表比较适合这种
  36. void SLTInsertAfter(SLTNode* pos, SLTDataType x);
  37. // 单链表删除pos位置之后的值,单链表比较适合这种
  38. void SLTEraseAfter(SLTNode* pos);
  39. // 不传头指针,在pos前插入x(也可以理解为在pos位置插入)
  40. void SLTInsertBefore1(SLTNode* pos, SLTDataType x);
  41. // 不传头指针,删除pos位置之前的值(也可以理解为删除pos位置的值)
  42. void SLTEraseBefore1(SLTNode* pos);
  43. // 单链表的销毁,不传二级
  44. void SLTDestroy(SLTNode* phead);
  45. // 单链表的销毁,传二级
  46. void SLTDestroy1(SLTNode** pphead);

在SList.c下

  1. #include"SList.h"//别忘了
  2. //链表的打印
  3. void SLTPrint(SLTNode* phead)
  4. {
  5. //assert(phead);这里并不需要断言phead不为空
  6. //为什么这里不需要断言?
  7. //空链表可以打印,即phead==NULL可以打印,直接断言就不合适了
  8. //那空顺序表也可以打印,那它为什么就要断言呢?
  9. //因为phead是指向第一个存有数据的结点的
  10. //而顺序表的ps是指向一个结构体
  11. SLTNode* cur = phead;//将phead赋值给cur,所以cur也指向第一个结点
  12. while (cur != NULL)//或while(cur)
  13. {
  14. printf("%d->", cur->data);//打印的时候加了个箭头更方便理解
  15. cur = cur->next;//next是下一个结点的地址
  16. //++cur/cur++;这种是不行的,指针加加,加到的是连续的下一个位置
  17. //链表的每一个结点都是单独malloc出来的,我们不能保证结点之间的地址是连续的
  18. }
  19. printf("NULL\n");
  20. }
  21. // 动态申请一个结点
  22. SLTNode* BuySLTNode(SLTDataType x)
  23. {
  24. //同样不需要断言
  25. SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));//先创建个结点
  26. if (newnode == NULL)//如果malloc失败
  27. {
  28. perror("malloc fail");
  29. return NULL;
  30. }
  31. //如果malloc成功
  32. newnode->data = x;//插入的数据
  33. newnode->next = NULL;//初始化为空
  34. return newnode;//返回newnode
  35. }
  36. // 单链表尾插
  37. void SLTPushBack(SLTNode** pphead, SLTDataType x)
  38. {
  39. assert(pphead);//pphead是plist的地址,不能为空
  40. //注意区分几个断言的判断,plist有可能是空,pphead一定不能为空
  41. SLTNode* newnode = BuySLTNode(x);
  42. //找尾(尾插之前先找到尾)
  43. if (*pphead == NULL)//若链表为空
  44. {
  45. *pphead = newnode;
  46. }
  47. else//若链表不为空
  48. {
  49. SLTNode* tail = *pphead;
  50. while (tail->next != NULL)
  51. //对于不为空的链表:尾插的本质是原尾结点要存新尾结点的地址
  52. {
  53. tail = tail->next;
  54. }
  55. tail->next = newnode;
  56. /*有些同学会写成:
  57. while (tail != NULL)
  58. {
  59. tail = tail->next;
  60. }
  61. tail = newnode;*/
  62. }
  63. }
  64. // 单链表的头插
  65. void SLTPushFront(SLTNode** pphead, SLTDataType x)
  66. {
  67. assert(pphead);//pphead是plist的地址,不能为空
  68. //发现plist不管是否为空,头插的方法都一样
  69. SLTNode* newnode = BuySLTNode(x);
  70. newnode->next = *pphead;
  71. *pphead = newnode;
  72. }
  73. // 单链表的尾删
  74. void SLTPopBack(SLTNode** pphead)
  75. {
  76. assert(pphead);//pphead是plist的地址,不能为空
  77. //检查有无结点
  78. assert(*pphead != NULL);//或者写成assert(*pphead);
  79. //1.只有一个结点
  80. if ((*pphead)->next == NULL)
  81. {
  82. free(*pphead);
  83. *pphead = NULL;
  84. }
  85. else
  86. {
  87. //2.有多个结点
  88. /*//法一:
  89. SLTNode* prev=NULL;
  90. SLTNode* tail = *pphead;
  91. while (tail->next != NULL)
  92. {
  93. prev = tail;
  94. tail = tail->next;
  95. }
  96. free(tail);
  97. tail = NULL;
  98. prev->next = NULL;*/
  99. //法二:
  100. SLTNode* tail = *pphead;
  101. while (tail->next->next != NULL)
  102. {
  103. tail = tail->next;
  104. }
  105. free(tail->next);
  106. tail->next = NULL;
  107. }
  108. }
  109. // 单链表头删
  110. void SLTPopFront(SLTNode** pphead)
  111. {
  112. assert(pphead);//pphead是plist的地址,不能为空
  113. //检查有无结点
  114. assert(*pphead != NULL);
  115. SLTNode* first = *pphead;
  116. *pphead = first->next;
  117. free(first);
  118. first = NULL;
  119. }
  120. // 单链表查找
  121. SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
  122. {
  123. SLTNode* cur = phead;//用cur去遍历,不用phead
  124. while (cur)//找x
  125. {
  126. if (cur->data == x)//如果找到了
  127. {
  128. return cur;
  129. }
  130. cur = cur->next;
  131. }
  132. return NULL;//如果找不到
  133. }
  134. //单链表在pos位置之前插入x(也可以理解为在pos位置插入)
  135. void SLTInsertBefore(SLTNode** pphead, SLTNode* pos, SLTDataType x)
  136. {
  137. assert(pos);//默认pos一定会找到
  138. assert(pphead);//pphead是plist的地址,不能为空
  139. if (pos == *pphead)//如果pos在第一个位置——那就是头插
  140. {
  141. SLTPushFront(pphead, x);
  142. }
  143. else//如果pos不是第一个位置
  144. {
  145. //找到pos的前一个位置
  146. SLTNode* prev = *pphead;
  147. while (prev->next != pos)
  148. {
  149. prev = prev->next;
  150. }
  151. SLTNode* newnode = BuySLTNode(x);
  152. prev->next = newnode;
  153. newnode->next = pos;
  154. }
  155. }
  156. // 单链表删除pos位置之前的值(也可以理解为删除pos位置的值)
  157. void SLTEraseBefore(SLTNode** pphead, SLTNode* pos)
  158. {
  159. assert(pphead);
  160. assert(pos);
  161. if (*pphead == pos)//如果pos在第一个位置
  162. {
  163. SLTPopFront(pphead);//头删
  164. }
  165. else//如果不在第一个位置
  166. {
  167. SLTNode* prev = *pphead;
  168. while (prev->next != pos)
  169. {
  170. prev = prev->next;
  171. }
  172. prev->next = pos->next;
  173. free(pos);
  174. //pos = NULL;形参的改变不影响实参,加不加这句话都可以
  175. }
  176. }
  177. //单链表在pos位置之后插入x
  178. void SLTInsertAfter(SLTNode* pos, SLTDataType x)
  179. {
  180. assert(pos);
  181. SLTNode* newnode = BuySLTNode(x);
  182. newnode->next = pos->next;
  183. pos->next = newnode;
  184. }
  185. // 单链表删除pos位置之后的值
  186. void SLTEraseAfter(SLTNode* pos)
  187. {
  188. assert(pos);
  189. assert(pos->next);
  190. SLTNode* del = pos->next;//保存要删除的结点
  191. pos->next = pos->next->next;//或者写成pos->next=del->next;
  192. free(del);
  193. del = NULL;
  194. }
  195. // 不传头指针,在pos前插入x(也可以理解为在pos位置插入)
  196. void SLTInsertBefore1(SLTNode* pos, SLTDataType x)
  197. {
  198. assert(pos);
  199. //调用单链表在pos位置之后插入x的函数
  200. SLTInsertAfter(pos, x);
  201. //交换pos和pos->next的值
  202. SLTDataType temp;
  203. temp = pos->data;
  204. pos->data = pos->next->data;
  205. pos->next->data = temp;
  206. }
  207. // 不传头指针,删除pos位置之前的值(也可以理解为删除pos位置的值)
  208. void SLTEraseBefore1(SLTNode* pos)
  209. {
  210. assert(pos);
  211. assert(pos->next);//不能尾删
  212. SLTNode* del = pos->next;
  213. pos->data = pos->next->data;
  214. pos->next = pos->next->next;
  215. free(del);
  216. del = NULL;
  217. }
  218. // 单链表的销毁
  219. void SLTDestroy(SLTNode* phead)
  220. {
  221. SLTNode* cur = phead;
  222. /*//有些人一开始会这样写
  223. while (cur)
  224. {
  225. //free不是销毁这个指针指向的内存,而是将指针指向的内存还给操作系统
  226. free(cur);//cur依旧指向free之前的地址
  227. cur = cur->next;
  228. }*/
  229. while (cur)
  230. {
  231. SLTNode* tmp = cur->next;
  232. free(cur);
  233. cur = tmp;
  234. }
  235. }
  236. // 单链表的销毁,传二级
  237. void SLTDestroy1(SLTNode** pphead)
  238. {
  239. assert(pphead);
  240. SLTNode* cur = *pphead;
  241. while (cur)
  242. {
  243. SLTNode* tmp = cur->next;
  244. free(cur);
  245. cur = tmp;
  246. }
  247. *pphead = NULL;
  248. }

在Test.c下

  1. #include"SList.h"
  2. void TestSList1()
  3. {
  4. SLTNode* plist = NULL;
  5. SLTPushBack(&plist, 1);
  6. SLTPushBack(&plist, 2);
  7. SLTPushBack(&plist, 3);
  8. SLTPushBack(&plist, 4);
  9. SLTPrint(plist);
  10. }
  11. void TestSList2()
  12. {
  13. SLTNode* plist = NULL;
  14. SLTPushFront(&plist, 1);
  15. SLTPushFront(&plist, 2);
  16. SLTPushFront(&plist, 3);
  17. SLTPushFront(&plist, 4);
  18. SLTPrint(plist);
  19. SLTPopBack(&plist);
  20. SLTPrint(plist);
  21. SLTPopBack(&plist);
  22. SLTPrint(plist);
  23. SLTPopBack(&plist);
  24. SLTPrint(plist);
  25. SLTPopBack(&plist);
  26. SLTPrint(plist);
  27. }
  28. TestSList3()
  29. {
  30. SLTNode* plist = NULL;
  31. SLTPushFront(&plist, 1);
  32. SLTPushFront(&plist, 2);
  33. SLTPushFront(&plist, 3);
  34. SLTPushFront(&plist, 4);
  35. SLTPrint(plist);
  36. SLTPopFront(&plist);
  37. SLTPrint(plist);
  38. SLTPopFront(&plist);
  39. SLTPrint(plist);
  40. SLTPopFront(&plist);
  41. SLTPrint(plist);
  42. SLTPopFront(&plist);
  43. SLTPrint(plist);
  44. }
  45. void TestSList4()
  46. {
  47. SLTNode* plist = NULL;
  48. SLTPushBack(&plist, 1);
  49. SLTPushBack(&plist, 2);
  50. SLTPushBack(&plist, 3);
  51. SLTPushBack(&plist, 4);
  52. SLTPrint(plist);
  53. //将寻找和修改结合
  54. //eg:值为2的结点*2
  55. SLTNode* ret = SLTFind(plist, 2);
  56. ret->data *= 2;
  57. SLTPrint(plist);
  58. }
  59. TestSList5()
  60. {
  61. SLTNode* plist = NULL;
  62. SLTPushBack(&plist, 1);
  63. SLTPushBack(&plist, 2);
  64. SLTPushBack(&plist, 3);
  65. SLTPushBack(&plist, 4);
  66. SLTPrint(plist);
  67. //寻找值为2的结点
  68. SLTNode* ret = SLTFind(plist, 2);
  69. SLTInsertBefore(&plist, ret, 20);//在该结点前插入值为20的结点
  70. SLTPrint(plist);
  71. }
  72. TestSList6()
  73. {
  74. SLTNode* plist = NULL;
  75. SLTPushBack(&plist, 1);
  76. SLTPushBack(&plist, 2);
  77. SLTPushBack(&plist, 3);
  78. SLTPushBack(&plist, 4);
  79. SLTPrint(plist);
  80. //寻找值为2的结点
  81. SLTNode* ret = SLTFind(plist, 2);
  82. SLTEraseBefore(&plist, ret);
  83. ret = NULL;//一般在这里置空
  84. SLTPrint(plist);
  85. }
  86. TestSList7()
  87. {
  88. SLTNode* plist = NULL;
  89. SLTPushBack(&plist, 1);
  90. SLTPushBack(&plist, 2);
  91. SLTPushBack(&plist, 3);
  92. SLTPushBack(&plist, 4);
  93. SLTPrint(plist);
  94. SLTNode* ret = SLTFind(plist, 2);
  95. SLTInsertAfter(ret, 20);
  96. SLTPrint(plist);
  97. }
  98. TestSList8()
  99. {
  100. SLTNode* plist = NULL;
  101. SLTPushBack(&plist, 1);
  102. SLTPushBack(&plist, 2);
  103. SLTPushBack(&plist, 3);
  104. SLTPushBack(&plist, 4);
  105. SLTPrint(plist);
  106. SLTNode* ret = SLTFind(plist, 2);
  107. SLTEraseAfter(ret);
  108. SLTPrint(plist);
  109. }
  110. TestSList9()
  111. {
  112. SLTNode* plist = NULL;
  113. SLTPushBack(&plist, 1);
  114. SLTPushBack(&plist, 2);
  115. SLTPushBack(&plist, 3);
  116. SLTPushBack(&plist, 4);
  117. SLTPrint(plist);
  118. SLTNode* ret = SLTFind(plist, 2);
  119. SLTInsertBefore1(ret, 20);
  120. SLTPrint(plist);
  121. }
  122. TestSList10()
  123. {
  124. SLTNode* plist = NULL;
  125. SLTPushBack(&plist, 1);
  126. SLTPushBack(&plist, 2);
  127. SLTPushBack(&plist, 3);
  128. SLTPushBack(&plist, 4);
  129. SLTPrint(plist);
  130. SLTNode* ret = SLTFind(plist, 2);
  131. SLTEraseBefore1(ret);
  132. SLTPrint(plist);
  133. }
  134. TestSList11()
  135. {
  136. SLTNode* plist = NULL;
  137. SLTPushBack(&plist, 1);
  138. SLTPushBack(&plist, 2);
  139. SLTPushBack(&plist, 3);
  140. SLTPushBack(&plist, 4);
  141. SLTPrint(plist);
  142. SLTDestroy(plist);
  143. plist = NULL;
  144. }
  145. TestSList12()
  146. {
  147. SLTNode* plist = NULL;
  148. SLTPushBack(&plist, 1);
  149. SLTPushBack(&plist, 2);
  150. SLTPushBack(&plist, 3);
  151. SLTPushBack(&plist, 4);
  152. SLTPrint(plist);
  153. SLTDestroy1(&plist);
  154. SLTPrint(plist);
  155. }
  156. int main()
  157. {
  158. //TestSList1();
  159. //TestSList2();
  160. //TestSList3();
  161. //TestSList4();
  162. //TestSList5();
  163. //TestSList6();
  164. //TestSList7();
  165. //TestSList8();
  166. //TestSList9();
  167. //TestSList10();
  168. //TestSList11();
  169. TestSList12();
  170. return 0;
  171. }

欢迎指正

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览45038 人正在系统学习中