• 无头单向非循环链表


    一、头文件

    Link.h这个头文件中包含了顺序表的定义和对顺序表进行操作的所有函数,所有的函数只传递结构体指针以加快程序运行速度。

    1. #pragma once//排除包含重复头文件
    2. #include
    3. #include
    4. #include//需要的头文件
    5. #define TYPE int
    6. typedef struct SListNode
    7. {
    8. TYPE data;
    9. struct SListNode* next;
    10. }SLNode;
    11. //动态申请一个节点
    12. SLNode* BuySListNode(TYPE x);
    13. // 单链表打印
    14. void SListPrint(SLNode* plist);
    15. // 单链表尾插
    16. void SListPushBack(SLNode** pplist,TYPE x);
    17. // 单链表的头插
    18. void SListPushFront(SLNode** pplist, TYPE x);
    19. // 单链表的尾删
    20. void SListPopBack(SLNode** pplist);
    21. // 单链表头删
    22. void SListPopFront(SLNode** pplist);
    23. // 单链表查找
    24. SLNode* SListFind(SLNode* plist, TYPE x);

    二、函数

    这些函数储存在一个function.c 文件中

    1.动态申请一个节点

    SLNode* BuySListNode(TYPE x);

    顾名思义,在堆区开辟一块空间用于储存数据x,之后通过其他函数链接到链表内

    1. //动态申请一个节点
    2. SLNode* BuySListNode(TYPE x)
    3. {
    4. SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
    5. if (newnode == NULL)//判空,为空则退出
    6. {
    7. perror("malloc fail");
    8. exit(-1);
    9. }
    10. newnode->data = x;//赋值
    11. newnode->next = NULL;
    12. return newnode;
    13. }

    2.单链表的头插

    void SListPushFront(SLNode** pplist, TYPE x);

    单链表的维护需要一个头指针指向第一个节点。而且头插还有两种情况,一种是还没有有效数据时的头插,一种是不同的头插

    注意:头插一定需要改变单独存储的头指针,所以改变地址就需要地址的地址,我们传递的参数就是二级指针

    1. // 单链表的头插
    2. void SListPushFront(SLNode** pplist, TYPE x)
    3. {
    4. assert(pplist);
    5. if (*pplist == NULL)//无数据头插就直接将节点的地址作为头指针
    6. {
    7. SLNode* code = BuySListNode(x);
    8. *pplist = code;
    9. }
    10. else
    11. {
    12. SLNode* frontcode = (SLNode*)malloc(sizeof(SLNode));
    13. frontcode->data = x;
    14. frontcode->next = *pplist;//新建节点与头部链接
    15. *pplist = frontcode;//更新头指针
    16. }
    17. }

    3.尾插

    void enlarge(SList* p);

    同样尾插也有两种情况,一种是还没有有效数据时的尾插,也就相当于头插;另一种是普通的尾插,先迭代寻找尾部,然后改变原尾部next指针为新节点地址,就直接将新节点与尾部链接了

    1. // 单链表尾插
    2. void SListPushBack(SLNode** pplist, TYPE x)
    3. {
    4. assert(pplist);
    5. if (*pplist == NULL)//若还没有数据尾插就是头插,头插就需要改变头指针,传二级指针
    6. {
    7. SLNode* code = BuySListNode(x);
    8. *pplist = code;
    9. }
    10. else
    11. {
    12. SLNode* cur = *pplist;
    13. while (cur->next != NULL)
    14. {
    15. cur = cur->next;//单链表只储存头指针,只能通过一个一个向后找才能迭代到尾部
    16. }
    17. SLNode* backcode = BuySListNode(x);//新建一个节点
    18. cur->next = backcode;//将末尾与新的节点链接起来
    19. backcode->next = NULL;//保证next不为野指针
    20. }
    21. }

    4.打印

    void SListPrint(SLNode* plist);

    迭代逐个打印直到尾部即可

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

    5.尾删

    void SListPopBack(SLNode* plist);

    *pplist表示就是头指针存储的地址,当链表为空时头地址为NULL也就达到当链表为空时,不进行删除的检查

    单链表的尾删分为链表只有一个节点的尾删和链表有多个节点的尾删两种情况

    一个节点:就只需要释放头节点,然后把头指针置空防止野指针

    多个节点:先迭代寻找尾部的前一个,然后用指针找到再将尾部释放,此时前一个就变成了最后一个。但是,此时的尾部的next依旧指向被释放的那个空间,我们就需要将这个节点的next置空

    1. // 单链表的尾删
    2. void SListPopBack(SLNode** pplist)
    3. {
    4. assert(pplist);
    5. assert(*pplist);//判断空
    6. SLNode* back = *pplist;//先把头节点赋给back指针
    7. //只有一个节点的尾删
    8. if ((*pplist)->next == NULL)
    9. {
    10. free(back);//释放头节点
    11. *pplist = NULL;//把头指针置空
    12. }
    13. //多个节点的尾删
    14. else
    15. {
    16. while (back->next->next != NULL)//迭代找倒数第二个节点
    17. {
    18. back = back->next;
    19. }
    20. free(back->next);//释放尾节点
    21. back->next = NULL;//置空野指针
    22. }
    23. }

    6.头删

    void SListPopFront(SLNode** pplist);

    *pplist表示就是头指针存储的地址,当链表为空时头地址为NULL也就达到当链表为空时,不进行删除的检查

    首先储存一下头指针,头指针改为第二个节点,释放储存的原首节点

    1. // 单链表头删
    2. void SListPopFront(SLNode** pplist)
    3. {
    4. assert(pplist);
    5. assert(*pplist);
    6. SLNode* code = *pplist;//定义指针指向头
    7. *pplist = code->next;//让头指针指向下一个位置
    8. free(code);
    9. code = NULL;//释放并置空
    10. }

    7.单链表查找

    void SListPopFront(SLNode** pplist);

    迭代查找数据,查找到就返回该节点的指针,找不到就返回空指针

    1. // 单链表查找
    2. SLNode* SListFind(SLNode* plist, TYPE x)
    3. {
    4. SLNode* cur = plist;
    5. while (cur->next != NULL)
    6. {
    7. if (cur->data == x)
    8. {
    9. return cur;//找到返回对应节点
    10. }
    11. cur = cur->next;//迭代
    12. }
    13. return NULL;//找不到返回NULL
    14. }

    8.特定位置前插

    void SListInsertbefore(SLNode** pplist, SLNode* pos, TYPE x);

    如果这个pos指向的位置是头,就直接头插;如果pos指向的位置是其他位置,建立一个prev指针指向pos前面的节点,首先建立一个新的节点并储存对应数据,先让新节点指向pos以保证不破坏链表的连续性然后把prev的next赋值为新节点就完成了pos前插

    1. // 在pos之前插入
    2. void SListInsertbefore(SLNode** pplist, SLNode* pos, TYPE x)
    3. {
    4. assert(pplist);
    5. assert(pos);
    6. if (pos == *pplist)//前插的对象为头,头插
    7. {
    8. SListPushFront(pplist, x);
    9. }
    10. else
    11. {
    12. SLNode* prev = *pplist;
    13. while (prev->next != pos)
    14. {
    15. prev = prev->next;
    16. assert(prev);//在查找完成后如果没有查找到pos,那么prev就会指向空位置,达到了检查的目的
    17. }
    18. SLNode* newnode = BuySListNode(x);
    19. prev->next = newnode;//先把该节点和它插入后的下一个链接,以防断开链表
    20. newnode->next = pos;//链接前面
    21. }
    22. }

    8.特定位置后插

    void SListInsertAfter(SLNode* pos, TYPE x);

    首先找到pos节点,建立一个节点并储存数据,将新节点与pos的下一个节点连接起来,最后将pos与新节点连接

    1. void SListInsertAfter(SLNode* pos, TYPE x)
    2. {
    3. assert(pos);
    4. SLNode* newnode = BuySListNode(x);
    5. newnode->next = pos->next;//此时原链表连续,先把新节点与pos链接
    6. pos->next = newnode;//将链表连接好
    7. }

    三、调试

    通过test.c来进行操作

    1. void test1()
    2. {
    3. SLNode* plist = NULL;
    4. plist = BuySListNode(1);
    5. SListPrint(plist);
    6. SListPushFront(&plist, 0);
    7. SListPrint(plist);
    8. SListPushBack(&plist, 2);
    9. SListPrint(plist);
    10. SListPopFront(&plist);
    11. SListPrint(plist);
    12. destoy(&plist);
    13. }
    14. void test2()
    15. {
    16. SLNode* plist = NULL;
    17. SListPushBack(&plist, 4);
    18. SListPushBack(&plist, 3);
    19. SListPushBack(&plist, 2);
    20. SListPushBack(&plist, 1);
    21. SListPrint(plist);
    22. printf("找到了:%d\n",SListFind(plist, 3)->data);
    23. SLNode* a = SListFind(plist, 3);
    24. SListPrint(plist);
    25. destoy(&plist);
    26. }
    27. int main()
    28. {
    29. //test1();
    30. test2();
    31. return 0;
    32. }

     

  • 相关阅读:
    MindSpore:测试mindspore的ascend用例编译失败
    LeNet-5网络结构详解和minist手写数字识别项目实践
    【测试】SonarLint连接SonarQube服务扫描
    Go测试学习
    免费开源库CDN加速对比
    Python - 定时自动获取 Bing 首页壁纸
    【Lychee图床】本地电脑搭建私人图床,公网远程访问
    AtCoder Beginner Contest 320 A/B/D
    umi3项目axios 请求参数序列化参数
    【Rust中的struct】
  • 原文地址:https://blog.csdn.net/qq_65285898/article/details/126552119