• 数据结构——单链表


    一、 链表的概念及结构

    1.1 链表的概念

    概念:链表是⼀种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表
    中的指针链接次序实现的 。
    链表的结构跟⽕⻋⻋厢相似,淡季时⻋次的⻋厢会相应减少,旺季时⻋次的⻋厢会额外增加⼏节。只 需要将⽕⻋⾥的某节⻋厢去掉/加上,不会影响其他⻋厢,每节⻋厢都是独⽴存在的。
    车厢是独⽴存在的,且每节⻋厢都有⻋⻔。想象⼀下这样的场景,假设每节⻋厢的⻋⻔都是锁上的状 态,需要不同的钥匙才能解锁,每次只能携带⼀把钥匙的情况下如何从⻋头⾛到⻋尾?
    最简单的做法:每节⻋厢⾥都放⼀把下⼀节⻋厢的钥匙。
    在链表⾥,每节“⻋厢”是什么样的呢?
    与顺序表不同的是,链表⾥的每节"⻋厢"都是独⽴申请下来的空间,我们称之为“结点/节点”
    节点的组成主要有两个部分: 当前节点要保存的数据  和  保存下⼀个节点的地址(指针变量)。
    图中指针变量 plist保存的是第⼀个节点的地址,我们称plist此时“指向”第⼀个节点,如果我们希 望plist“指向”第⼆个节点时,只需要修改plist保存的内容为0x0012FFA0.

    为什么还需要指针变量来保存下⼀个节点的位置?

    链表中每个节点都是独立申请的(即需要插⼊数据时才去申请⼀块节点的空间),我们需要通过指针变量来保存下⼀个节点位置才能从当前节点找到下⼀个节点。
     

    1.2 链表的结构

    结合前⾯学到的结构体知识,我们可以给出每个节点对应的结构体代码:

    假设当前保存的节点为整型:

    1. struct SListNode
    2. {
    3. int data; //节点数据
    4. struct SListNode* next; //指针变量⽤保存下⼀个节点的地址
    5. };

    二、单链表的实现

    2.1  单链表的尾插和头插

    2.1.1  SList.h

    1. #define _CRT_SECURE_NO_WARNINGS
    2. typedef int SListDateType;
    3. typedef struct SListNode
    4. {
    5.     SListDateType val;
    6.     struct SListNode* next;
    7.     
    8. }SLNode;
    9. //没有初始化,若链表为空,则插入的第一个节点为头节点
    10. //头插和尾插
    11. void SLPushBack(SLNode** pphead,SListDateType x);
    12. void SLPushFront(SLNode** pphead,SListDateType x);

    2.1.2   SList.c

    尾插:

    思路一: 若头节点是NULL,插入的第一个节点为头节点;

    判断当前节点的下一节点是否为空,若为空,则为尾节点,在尾节点后面插入新节点

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include"SList.h"
    3. #include
    4. #include
    5. #include
    6. //申请一个节点
    7. SLNode* SLBuyNode(SListDateType x)
    8. {
    9. SLNode* node = (SLNode*)malloc(sizeof(SLNode));
    10. if (node == NULL)
    11. {
    12. perror("malloc fail:");
    13. return NULL;
    14. }
    15. node->val = x;
    16. node->next = NULL;
    17. return node;
    18. }
    19. void SLPushBack(SLNode** pphead, SListDateType x)
    20. {
    21. assert(pphead);
    22. SLNode* node = SLBuyNode(x);
    23. SLNode* pcur = *pphead;
    24. if ((*pphead) == NULL)
    25. {
    26. *pphead = node;
    27. return 1;
    28. }
    29. while (pcur->next != NULL)
    30. {
    31. pcur = pcur->next;
    32. }
    33. pcur->next = node;
    34. }

    尾插:

    思路二:若头节点是NULL,插入的第一个节点为头节点;

    若第一个节点不是空,说明链表至少一个节点,pcur为当前节点,prve为前一个节点,判断pcur是否为空,若为空,则prve可定位到尾节点插入新节点

    1. void SLPushBack(SLNode** pphead, SListDateType x)
    2. {
    3. assert(pphead);
    4. SLNode* node = SLBuyNode(x);
    5. SLNode* pcur = *pphead;
    6. SLNode* prev = NULL;
    7. if ((*pphead) == NULL)
    8. {
    9. *pphead = node;
    10. return 1;
    11. }
    12. while (pcur)
    13. {
    14. prev = pcur;
    15. pcur = pcur->next;
    16. }
    17. prev->next = node;
    18. }

    头插:

    注:在这里头指针改变了指向,所以传的二级指针

    1. void SLPushFront(SLNode** pphead,SListDateType x)
    2. {
    3. assert(pphead);
    4. SLNode* node = SLBuyNode(x);
    5. node->next = *pphead;
    6. *pphead = node;
    7. }

    2.2  单链表的头删和尾删

    2.2.1  SList.h

    1. //头删和尾删
    2. void SLPopBack(SLNode** pphead);
    3. void SLPopFront(SLNode** pphead);

    2.2.2  SList.c

    尾删:

    首先保证至少有一个节点

    1)若只有一个节点,释放该节点空间,使指向该空间的头节点置为空

    2)若有俩个及以上节点,pcur为当前节点,prve为前一个节点,判断pcur的下一个节点是否为空,若为空,则prve可定位到次尾节点,删除尾节点,使次尾节点置为空

    1. void SLPopBack(SLNode** pphead)
    2. {
    3. assert(pphead && *pphead);//至少有一个节点
    4. SLNode* del = *pphead;
    5. SLNode* prev = NULL;
    6. //若只有一个节点
    7. if (del->next == NULL)
    8. {
    9. free(del);
    10. del = NULL;
    11. return 1;
    12. }
    13. //不止一个节点
    14. while (del->next)
    15. {
    16. prev = del;
    17. del = del->next;
    18. }
    19. prev->next = NULL;
    20. free(del);
    21. del = NULL;
    22. }

    头删:

    1. void SLPopFront(SLNode** pphead)
    2. {
    3. assert(pphead && *pphead);//至少有一个节点
    4. SLNode* del = *pphead;
    5. *pphead = (*pphead)->next;
    6. free(del);
    7. del = NULL;
    8. }

     2.3   打印链表

    2.2.1  SList.h

    1. //打印数据
    2. void SLPrint(SLNode* pphead);

    2.3.2 SList.c

    1. void SLPrint(SLNode* pphead)
    2. {
    3.     assert(pphead);
    4.     SLNode* pcur = pphead;
    5.     while(pcur)
    6.     {
    7.         printf("%d->", pcur->val);
    8.         pcur = pcur->next;
    9.     }
    10.     printf("NULL\n");
    11. }

    2.4   在指定位置之前插入数据和指定位置之后插入数据

    2.4.1  SList.h

    1. //在指定位置之前插入数据和指定位置之后插入数据
    2. void SLInsertFront(SLNode** pphead, SLNode* pos, SListDateType x);
    3. void SLInsertAfter(SLNode** pphead, SLNode* pos, SListDateType x);

    2.4.2 SList.c

    1. void SLInsertFront(SLNode** pphead, SLNode* pos, SListDateType x)
    2. {
    3. assert(pphead && *pphead && pos);
    4. //若pos为第一个节点或者链表只有一个节点,->头插
    5. SLNode* node = SLBuyNode(x);
    6. SLNode* prev =*pphead;
    7. if (pos == *pphead)
    8. {
    9. node->next = *pphead;
    10. *pphead = node;
    11. }
    12. else//pose为第2,3,4...节点
    13. {
    14. while (prev->next != pos)
    15. {
    16. prev = prev->next;
    17. }
    18. node->next = pos;
    19. prev->next = node;
    20. }
    21. }
    22. void SLInsertAfter(SLNode** pphead, SLNode* pos, SListDateType x)
    23. {
    24. assert(pphead&&pos&&*pphead);
    25. SLNode* node = SLBuyNode(x);
    26. node->next = pos->next;
    27. pos->next = node;
    28. }

    2.5  在指定位置删除数据和指定位置之后删除数据

    2.5.1 List.h

    1. //在指定位置删除数据和指定位置之后删除数据
    2. void SLErase(SLNode** pphead, SLNode* pos);
    3. void SLEraseAfter(SLNode** pphead, SLNode* pos);

    2.5.1 List.c

    1. void SLErase(SLNode** pphead, SLNode* pos)
    2. {
    3. assert(pphead && *pphead && pos);
    4. SLNode* prev = *pphead;
    5. //pos为第一个节点或者链表只有一个节点
    6. if (pos == *pphead)
    7. {
    8. *pphead=(*pphead)->next;
    9. free(pos);
    10. pos = NULL;
    11. }
    12. else//为第2,3,4....个节点
    13. {
    14. while (prev->next != pos)
    15. {
    16. prev = prev->next;
    17. }
    18. prev->next = pos->next;
    19. free(pos);
    20. pos = NULL;
    21. }
    22. }
    23. void SLEraseAfter(SLNode** pphead, SLNode* pos)
    24. {
    25. assert(pphead && *pphead && pos);
    26. assert((*pphead)->next);
    27. assert(pos->next);
    28. SLNode* del = pos->next;
    29. pos->next = del->next;
    30. free(del);
    31. del = NULL;
    32. }

    2.6  查找数据

    2.6.1  List.h

    1. //查找数据
    2. SLNode* SLFind(SLNode** pphed, SListDateType x);

    2.6.2  List.c

    1. SLNode* SLFind(SLNode** pphead,SListDateType x)
    2. {
    3. assert(pphead);
    4. SLNode* pcur = *pphead;
    5. while (pcur)
    6. {
    7. if (pcur->val == x)
    8. {
    9. return pcur;
    10. }
    11. pcur = pcur->next;
    12. }
    13. return NULL;
    14. }

    2.7    销毁

    思路:销毁时,会使链表断裂,所以在销毁当前节点时,需要提前保存好下一个节点

    2.7.1  SList.h 

    1. //销毁
    2. void SLDestroy(SLNode** pphead);

    2.7.2  SList.c

    1. void SLDestroy(SLNode** pphead)
    2. {
    3. assert(pphead&&*pphead);
    4. SLNode* pcur = *pphead;
    5. SLNode* next = (*pphead)->next;
    6. while(pcur)
    7. {
    8. free(pcur);
    9. pcur = next;
    10. if (next)
    11. {
    12. next = next->next;
    13. }
    14. }
    15. *pphead = NULL;
    16. }

    2.8   测试代码

    2.8.1  text.c

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include"SList.h"
    3. #include
    4. int main()
    5. {
    6. SLNode* s = NULL;
    7. SLPushBack(&s,1);
    8. SLPushBack(&s, 2);
    9. SLPushBack(&s, 3);
    10. SLPushBack(&s, 4);
    11. SLPrint(s);
    12. SLNode*node= SLFind(&s, 2);
    13. SLInsertFront(&s,node, 77);
    14. SLPrint(s);
    15. node = SLFind(&s, 4);
    16. SLInsertAfter(&s, node, 55);
    17. SLPrint(s);
    18. //在指定位置删除数据和指定位置之后删除数据
    19. SLErase(&s, node);
    20. SLPrint(s);
    21. node = SLFind(&s, 1);
    22. SLEraseAfter(&s, node);
    23. SLPrint(s);
    24. //查找数据
    25. //
    26. // SLPushFront(&s, 5);
    27. // SLPrint(s);
    28. //
    29. // //头删和尾删
    30. // SLPopBack(&s);
    31. // SLPrint(s);
    32. //
    33. // SLPopFront(&s);
    34. //SLPrint(s);
    35. return 0;
    36. }

    测试结果:

  • 相关阅读:
    基于混沌搜索策略的鲸鱼优化算法-附代码
    小册上新 | 掌握 SpringBoot 场景整合,成为开发多面手!
    ​Unity Vuforia 新手(图片识别)教程,后续整理 实体识别 详细流程
    【附源码】Python计算机毕业设计社区论坛
    ​iOS上架App Store的全攻略
    golang设计模式——适配器模式
    HK32F030MF4P6 EEPROM例程
    Anaconda3安装部署(二) 百篇文章学PyQT
    step6:改用单例模式
    Kafka 社区KIP-382中文译文(MirrorMaker2/集群复制/高可用/灾难恢复)
  • 原文地址:https://blog.csdn.net/zhoubancheng/article/details/134210636