当前位置: 首页 > news >正文

单链表详解

单链表

  • 一.概念
    • 二.一些类型的创建
    • 三.尾插
    • 四.头插
  • 五.头删尾删
  • 六.打印链表
  • 七.单链表查找,任意位置插入,任意位置删除
  • 八.源代码

一.概念

该篇链表博客是按照工程项目的格式来记录的,与平常的算法链表有些许不同,注意区分。

在这里插入图片描述

在这里插入图片描述

二.一些类型的创建

在这里插入图片描述

在这里插入图片描述

三.尾插

因为需要凭空插入一个数,所以我们肯定需要新开辟一个节点(newnode)来存放需要插入的数。之后我们需要找到原链表的尾部再将其插入就可以了。

这里尤其需要注意的一个点是:我们增加操作如果链表为空时需要改变头节点(phead),因为phead是一级指针,所以我们传参需要二级指针。

Test.c

在这里插入图片描述

SList.h
在这里插入图片描述

SList.c

在这里插入图片描述

四.头插

因为头插必定会改变头节点所以同样需要使用二级指针。同时让插入的节点变为头节点。

Test.c

在这里插入图片描述

SLIst.h

在这里插入图片描述

SList.c

在这里插入图片描述

五.头删尾删

SList.h

在这里插入图片描述

SList.c

尾删
在这里插入图片描述

头删

在这里插入图片描述

六.打印链表

SList.h

在这里插入图片描述

SList.c

在这里插入图片描述

七.单链表查找,任意位置插入,任意位置删除

SList.h

在这里插入图片描述

SLiist.c

查找

在这里插入图片描述

在pos位置前面插入

在这里插入图片描述

将pos位置删除

在这里插入图片描述

八.源代码

test.c

#include"SList.h"void TestList()
{SListNode* phead = NULL;//测试部分}int main()
{TestList();return 0;
}

SList.h

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>#define SLTDataType inttypedef struct {SLTDataType data;//这里将int类型替换成链表类型以符合工程书写习惯struct SListNode* next;
}SListNode;void SLPushBack(SListNode** phead,SLTDataType x);//尾插void SLPushFront(SListNode** phead, SLTDataType x);//头插void SLPopBack(SListNode** phead);//尾删void SLPopFront(SListNode** phead);//头删void SLPrint(SListNode* phead);//打印//查找
SListNode* SListFind(SListNode* phead, SLTDataType x);
void SListInsert(SListNode**pphead,SListNode* pos, SLTDataType x);//在pos位置前位置插入(注意pos是节点的指针)
void SListErase(SListNode**pphead,SListNode* pos);//删除pos位置

SList.c

#include"SList.h"void SLPushBack(SListNode** pphead, SLTDataType x)//尾插
{SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));//开辟新节点来存放插入的数if (newnode == NULL)//如果开辟失败则返回错误{perror("malloc fail");return;}newnode->data = x;newnode->next = NULL;//初始化新节点if (*pphead == NULL)//注意*pphead就是phead{*pphead = newnode;}//如果链表里没有数直接插入数else{SListNode* tail = *pphead;while (tail->next != NULL){tail = tail->next;}//找到原链表尾部tail->next = newnode;//插入新节点}}void SLPushFront(SListNode** pphead, SLTDataType x)//头插
{SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));//开辟一个新节点if (newnode == NULL)//判断是否开辟成功{perror("malloc fail");}newnode->data = x;newnode->next = *pphead;//让新的节点指向原本的头节点*pphead = newnode;//让新节点成为头节点
}void SLPopBack(SListNode** pphead)//尾删
{assert(*pphead!=NULL);//断言链表不能为空//找尾if ((*pphead)->next == NULL)//如果链表只有一个数{free(*pphead);//直接释放头节点*pphead = NULL;}else{SListNode* tail = *pphead;SListNode* prev = NULL;while (tail->next != NULL){prev = tail;tail = tail->next;}free(tail);//直接删掉tail节点tail = NULL;prev->next = NULL;}}void SLPopFront(SListNode** pphead)//头删
{assert(*pphead != NULL);//链表不能为空SListNode* first = *pphead;*pphead = first->next;//直接让头节点变为第二个数free(first);first = NULL;
}void SLPrint(SListNode* phead)//打印
{SListNode* start = phead;while (start != NULL){printf("%d ", start->data);start = start->next;}printf("NULL");
}SListNode* SListFind(SListNode* phead, SLTDataType x)//查找
{SListNode* cur = phead;while (cur->data != x&&cur!=NULL){cur = cur->next;}return cur;
}void SListInsert(SListNode**pphead,SListNode* pos, SLTDataType x)//任意位置插入
{if (pos == *pphead){SLPushFront(pphead,x);//头插}else{SListNode* pre = *pphead;while (pre->next != pos)//找到pos前面的位置{pre = pre->next;}SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));//开辟新节点newnode->data = x;newnode->next = pos;//初始化新节点pre->next = newnode;//将pos前一个位置连接到新节点上}
}void SListErase(SListNode** pphead, SListNode* pos)//任意位置删除
{if (pos == *pphead){SLPopFront(pphead);//头删 }else{SListNode* pre = *pphead;while (pre->next != pos)//找到pos前面的位置{pre = pre->next;}pre->next = pos->next;//删除free(pos);}
}
http://www.lryc.cn/news/27371.html

相关文章:

  • 【AUTOSAR-CanNM】-3.1-如何让ECU发出的首帧是NM帧(Tx Nm报文先于Tx App应用报文发出)
  • html常用标签2和语法练习
  • 【go语言之thrift协议三之client端分析】
  • Codeforces Round #855 (Div. 3) A-E
  • 3/3操作系统作业
  • 「C/C++」 标准文件操作大全
  • 一款SAST工具需要支持多少种编译器呢?
  • jvm mat分析dump文件
  • python16行代码获取原神全角色+全语音
  • 链接投票二维码制作制作投票链接视频选举投票制作
  • HTTP 协议
  • 公司新招了个人,一副毛头小子的样儿,哪想到是新一代卷王····
  • TSDF学习记录
  • 【Linux】孤儿进程
  • ChatGPT解答:python大批量读写ini文件时,性能很低,有什么解决方法吗,给出具体的思路和实例
  • MySql主键id不推荐使用UUID
  • 密码算法(SM1、SM2、SM3、SM4、同态加密、密态计算、隐私计算和安全多方计算)
  • 保险行业中【内容行政系统】模块功能的实现
  • C语言入门知识——(7)VS2022的C语言基础调试
  • 数据库可视化开发工具内容介绍
  • 坚如磐石:TiDB 基于时间点的恢复(PiTR)特性优化之路丨6.5 新特性解析
  • 【云原生】K8S中PV和PVC
  • 24小时稳定性爆肝测试!国内外5款远程控制软件大盘点
  • 【Java集合框架】篇三:List接口
  • 【算法经典题集】二分(持续更新~~~)
  • 【c++】2023杭州月薪个税计算(chatGPT帮忙加注释)
  • 【TypeScript】的上手学习指南!
  • 红黑树(Insert())
  • MOV指令使用
  • 解释一下RecyclerView的适配器内部方法