单链表详解

news/2025/2/23 6:14:20/

单链表

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

一.概念

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

在这里插入图片描述

在这里插入图片描述

二.一些类型的创建

在这里插入图片描述

在这里插入图片描述

三.尾插

因为需要凭空插入一个数,所以我们肯定需要新开辟一个节点(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.ppmy.cn/news/29708.html

相关文章

Leaflet基础入门教程(一)

leaflet是一个前端的轻量的gis框架,为什么说它轻量呢。因为相比于传统的“庞大的”GIS框架比如openlayers和mapbox,leaflet不仅代码体积小,而且API构成也极为简单。是GIS行业小白入门级别学习的最好的框架,没有之一。 那么话不多说我们首先来学习一下如何使用leaflet搭建一…

Vulnhub靶场----9、DC-9

文章目录一、环境搭建二、渗透流程三、思路总结一、环境搭建 DC-9下载地址&#xff1a;https://download.vulnhub.com/dc/DC-9.zip kali&#xff1a;192.168.144.148 DC-9&#xff1a;192.168.144.158 二、渗透流程 1、信息收集nmap -T5 -A -p- -sV -sT 192.168.144.158思路&am…

首版次软件产品认证/山东省首版次高端软件申报通知

各市工业和信息化局&#xff1a; 为贯彻落实《山东省人民政府办公厅关于加快推动软件产业高质量发展的实施意见》(鲁政办发〔〕1号)&#xff0c;加速提升软件供给能力&#xff0c;努力打造山东软件“名品”&#xff0c;构建高端软件产业生态&#xff0c;现组织开展第七批山东省…

k8s编程operator实战之云编码平台——⑤项目完成、部署

文章目录1、效果展示2、保存用户状态和访问用户服务实现方案2.1 如何保存用户的状态2.1.1 解决保留安装的插件问题2.2 如何访问到用户在工作空间中启动的http服务2.2.1 code-server如何帮我们实现了用户程序的代理3、Operator功能实现3.1 使用KubeBuilder创建项目3.1.1 完善kin…

存在重复元素模块-三道题

文章目录存在重复元素217. 存在重复元素219. 存在重复元素 II220. 存在重复元素 III &#xff08;SortedList二分&#xff09;小结存在重复元素 217. 存在重复元素 题目链接&#xff1a;217. 存在重复元素 题目大意&#xff1a;给你一个整数数组 nums 。如果任一值在数组中出…

DatenLord前沿技术分享 No.19

达坦科技专注于打造新一代开源跨云存储平台DatenLord&#xff0c;致力于解决多云架构、多数据中心场景下异构存储、数据统一管理需求等问题&#xff0c;以满足不同行业客户对海量数据跨云、跨数据中心高性能访问的需求。GPU编程可以大幅提升计算速度和效率&#xff0c;从而使得…

C语言传递数组给函数从函数返回数组指向数组的指针

如果您想要在函数中传递一个一维数组作为参数&#xff0c;您必须以下面三种方式来声明函数形式参数&#xff0c;这三种声明方式的结果是一样的&#xff0c;因为每种方式都会告诉编译器将要接收一个整型指针。同样地&#xff0c;您也可以传递一个多维数组作为形式参数。 方式 1…

Ant design Chart onReady函数使用外部变量问题

一、问题描述封装了一个Chart组件&#xff0c;它接收一个boolean类型的props&#xff0c;根据这个boolean的true或false执行不同的操作。经过console.log验证&#xff0c;onReady函数只会在组件初次渲染时取到props值&#xff0c;不管后面的props变化成什么都无法重新取值。二、…