手搓单链表(无哨兵位)(C语言)

目录

SLT.h

SLT.c

SLTtest.c

测试示例

单链表优劣分析


SLT.h

#pragma once

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef int SLTDataType;

typedef struct SListNode
{
	SLTDataType data;
	struct SListNode* next;
}SLTNode;

//打印单链表
void SLTPrint(SLTNode* phead);
//创建节点
SLTNode* BuySLTNode(SLTDataType x);
//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x);
//尾删
void SLTPopBack(SLTNode** pphead);
//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x);
//头删
void SLTPopFront(SLTNode** pphead);

//单链表查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
//在pos之前插入
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//删除pos位置
void SLTErase(SLTNode** pphead, SLTNode* pos);

//单链表在pos位置之后插入x
void SListInsertAfter(SLTNode* pos, SLTDataType x);
//单链表删除pos位置之后的值
void SListEraseAfter(SLTNode* pos);

//销毁单链表
void SLTDestroy(SLTNode** pphead);

SLT.c

#include "SLT.h"

//打印单链表
void SLTPrint(SLTNode* phead)
{
	SLTNode* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

//创建节点
SLTNode* BuySLTNode(SLTDataType x)
{
	SLTNode* node = (SLTNode*)malloc(sizeof(SLTNode));
	if (node == NULL)
	{
		perror("SLTBuysNode");
		exit(-1);
	}
	node->data = x;
	node->next = NULL;
	return node;
}

//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);

	if (*pphead == NULL)
	{
		*pphead = BuySLTNode(x);
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = BuySLTNode(x);
	}
}

//尾删
void SLTPopBack(SLTNode** pphead)
{
	assert(pphead);
	assert(*pphead);

	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next->next)
		{
			tail = tail->next;
		}
		free(tail->next->next);
		tail->next = NULL;
	}
}

//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

//头删
void SLTPopFront(SLTNode** pphead)
{
	assert(pphead);
	assert(*pphead);

	SLTNode* newhead = (*pphead)->next;
	free(*pphead);
	*pphead = newhead;
}

// 单链表查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			//找到了
			return cur;
		}
		cur = cur->next;
	}
	//找不到
	return NULL;
}

//在pos之前插入
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pphead);

	if (*pphead == pos)
	{
		SLTNode* newnode = BuySLTNode(x);
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		SLTNode* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		SLTNode* newnode = BuySLTNode(x);
		cur->next = newnode;
		newnode->next = pos;
	}
	
}

// 删除pos位置
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pphead);
	assert(*pphead && pos);

	if (*pphead == pos)
	{
		SLTNode* newhead = (*pphead)->next;
		free(*pphead);
		*pphead = newhead;
	}
	else
	{
		SLTNode* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		SLTNode* pos_next = pos->next;
		free(pos);
		cur->next = pos_next;
	}
	
}

// 单链表在pos位置之后插入x
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
	assert(pos);

	SLTNode* newnode = BuySLTNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

// 单链表删除pos位置之后的值
void SListEraseAfter(SLTNode* pos)
{
	assert(pos && pos->next);

	SLTNode* pos_next_next = pos->next->next;
	free(pos->next);
	pos->next = pos_next_next;
}

//销毁单链表
void SLTDestroy(SLTNode** pphead)
{
	assert(pphead);
	assert(*pphead);

	while (*pphead)
	{
		SLTNode* newhead = (*pphead)->next;
		free(*pphead);
		*pphead = newhead;
	}
}

SLTtest.c

#include "SLT.h"

void SLTtest1()
{
	SLTNode* phead = NULL;
	SLTPrint(phead);
	SLTPushBack(&phead, 1);
	SLTPushBack(&phead, 2);
	SLTPushBack(&phead, 3);
	SLTPushBack(&phead, 4);
	SLTPushBack(&phead, 5);
	SLTPrint(phead);

	SLTPopBack(&phead);
	SLTPrint(phead);
	SLTPopBack(&phead);
	SLTPrint(phead);
	SLTPopBack(&phead);
	SLTPrint(phead);
	SLTPopBack(&phead);
	SLTPrint(phead);
	SLTPopBack(&phead);
	SLTPrint(phead);
	/*SLTPopBack(&phead);
	SLTPrint(phead);*/
}
void SLTtest2()
{
	SLTNode* phead = NULL;
	SLTPrint(phead);
	SLTPushFront(&phead, 1);
	SLTPushFront(&phead, 2);
	SLTPushFront(&phead, 3);
	SLTPushFront(&phead, 4);
	SLTPushFront(&phead, 5);
	SLTPrint(phead);

	/*SLTDestroy(&phead);
	SLTPrint(phead);*/

	/*SLTPopFront(&phead);
	SLTPrint(phead);
	SLTPopFront(&phead);
	SLTPrint(phead);
	SLTPopFront(&phead);
	SLTPrint(phead);
	SLTPopFront(&phead);
	SLTPrint(phead);
	SLTPopFront(&phead);
	SLTPrint(phead);*/
	/*SLTPopFront(&phead);
	SLTPrint(phead);*/
}
void SLTtest3()
{
	SLTNode* phead = NULL;
	SLTPrint(phead);
	SLTPushFront(&phead, 1);
	SLTPushFront(&phead, 2);
	SLTPushFront(&phead, 3);
	SLTPushFront(&phead, 4);
	SLTPushFront(&phead, 5);
	SLTPrint(phead);
	//在pos位置之前插入删除
	SLTInsert(&phead, phead, 10);
	SLTPrint(phead);

	SLTInsert(&phead, NULL, 20);
	SLTPrint(phead);

	SLTNode* pos = SLTFind(phead, 3);
	SLTInsert(&phead, pos, 30);
	SLTPrint(phead);

	//删除pos位置的值
	SLTErase(&phead, phead);
	SLTPrint(phead);

	pos = SLTFind(phead, 3);
	SLTErase(&phead, pos);
	SLTPrint(phead);

	pos = SLTFind(phead, 20);
	SLTErase(&phead, pos);
	SLTPrint(phead);
}

void SLTtest4()
{
	SLTNode* phead = NULL;
	SLTPrint(phead);
	SLTPushFront(&phead, 1);
	SLTPushFront(&phead, 2);
	SLTPushFront(&phead, 3);
	SLTPushFront(&phead, 4);
	SLTPushFront(&phead, 5);
	SLTPrint(phead);
	//在pos位置之后插入
	SListInsertAfter(phead, 10);
	SLTPrint(phead);

	SLTNode* pos = SLTFind(phead, 3);
	SListInsertAfter(pos, 30);
	SLTPrint(phead);

	pos = SLTFind(phead, 1);
	SListInsertAfter(pos, 10);
	SLTPrint(phead);

	//删除pos位置之后的值
	SListEraseAfter(phead);
	SLTPrint(phead);

	pos = SLTFind(phead, 4);
	SListEraseAfter(pos);
	SLTPrint(phead);
	
}
int main()
{
	//测试尾插尾删
	//SLTtest1();
	
	//测试头插头删
	//SLTtest2();
	
	//在pos位置之前插入删除
	//SLTtest3();

	//在pos位置之后插入删除
	//SLTtest4();
	return 0;
}

测试示例

尾插尾删:

头插头删:

在pos位置之前插入:

删除pos位置的值:

在pos位置之后插入删除:

单链表优劣分析

        单链表在逻辑上连续,在物理上不一定连续,用多少申请多少空间,因此不存在空间浪费且申请空间开销小;插入删除元素时无需挪动元素,只需要改变指向即可,但除头插头删,在pos位置之后插入删除外需要先遍历链表,效率低下;不支持随机访问;缓存利用率低。

        总结:单链表具有较大的缺陷,相较于顺序表而言没有明显的优势,因此在实践中几乎没有应用场景。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/576038.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

数据治理和数据管理 傻傻分不清楚?

互联网时代&#xff0c;数据&#xff0c;这一无形资产&#xff0c;已成为现代企业的核心竞争力。如何高效地管理和利用数据&#xff0c;成为企业关注的焦点。在这个过程中&#xff0c;数据治理&#xff08;Data Governance&#xff09;和数据管理&#xff08;Data Management&a…

1分钟掌握 Python 函数参数

任何编程语言函数都是非常重要的一部分&#xff0c;而在进行函数调用时&#xff0c;了解函数的参数传递方式是非常有必要的。Python中支持哪些传参方式呢&#xff1f; Python中的传参方式是比较灵活的&#xff0c;主要包括以下六种&#xff1a; 按照位置传参按照关键字传参默…

【算法基础实验】图论-构建无向图

构建无向图 前提 JAVA实验环境 理论 无向图的数据结构为邻接表数组&#xff0c;每个数组中保存一个Bag抽象数据类型&#xff08;Bag类型需要专门讲解&#xff09; 实验数据 我们的实验数据是13个节点和13条边组成的无向图&#xff0c;由一个txt文件来保存&#xff0c;本…

网贷大数据黑名单要多久才能变正常?

网贷大数据黑名单是指个人在网贷平台申请贷款时&#xff0c;因为信用记录较差而被列入黑名单&#xff0c;无法获得贷款或者贷款额度受到限制的情况。网贷大数据黑名单的具体时间因个人信用状况、所属平台政策以及银行审核标准不同而异&#xff0c;一般来说&#xff0c;需要一定…

森林消防泵柱塞泵工作原理深度解析——恒峰智慧科技

森林是地球上重要的生态系统&#xff0c;而森林火灾则是这一生态系统面临的主要威胁之一。为了有效应对森林火灾&#xff0c;森林消防泵成为了不可或缺的灭火工具。其中&#xff0c;柱塞泵作为森林消防泵的核心部件&#xff0c;其工作原理的理解对于提高森林消防效率具有重要意…

Java面试八股文-2024

面试指南 TMD&#xff0c;一个后端为什么要了解那么多的知识&#xff0c;真是服了。啥啥都得了解 MySQL MySQL索引可能在以下几种情况下失效&#xff1a; 不遵循最左匹配原则&#xff1a;在联合索引中&#xff0c;如果没有使用索引的最左前缀&#xff0c;即查询条件中没有包含…

Javascript 插值搜索与二分搜索

插值搜索和二分搜索都是在有序数组中查找目标元素的算法。它们之间的核心区别在于确定中间元素的方式。 1、二分搜索&#xff08;Binary Search&#xff09;&#xff1a;二分搜索是一种通过将目标值与数组中间元素进行比较&#xff0c;然后根据比较结果缩小搜索范围的算…

ubuntu16安装docker及docker-compose

ubuntu16安装docker及docker-compose 一、环境前期准备 检查系统版本 系统版本最好在16及以上&#xff0c;可以确保系统的兼容性 lsb_release -a查看内核版本及系统架构 建议用 x86_64的系统架构&#xff0c;安装是比较顺利的 uname -a32的系统不支持docker&#xff0c;安…

Adipogen--Progranulin (rat) ELISA Kit

Progranulin (PGRN)是一种广泛表达的多能生长因子&#xff0c;通过激活控制细胞周期进展和细胞运动的信号级联反应&#xff0c;在发育、创伤修复和炎症等过程中发挥作用。它在中枢神经系统中的功能值得关注&#xff0c;因为在额颞退行性变(FTLD)病例中发现了PGRN基因突变。此外…

2024年618有哪些数码家电值得入手?全网最省钱攻略指南

作为全年唯一设在夏季的大型电商狂欢节&#xff0c;618一直是很多人购置数码类、家电类的最好时间节点之一。但是问题来了&#xff0c;现在的数码家电行业“鱼龙混杂”&#xff0c;不仅越来越多新品牌涌入市场&#xff0c;而且各个大品牌为了抢占市场&#xff0c;旗下产品的品类…

ArcGIS专题图制作—3D海底地形

这一期的制图教程将带我们走入马里亚纳海沟&#xff0c;让我们一起绘制这张美妙的地图吧&#xff01;视频也上传到了B站&#xff0c;小伙伴可以去支持一下。 【制图教程】ArcGIS Pro绘制3D海底地形图 B站视频链接&#xff1a;【制图教程】ArcGIS Pro绘制3D海底地形图_哔哩哔哩…

7-31 字符串循环左移

题目链接&#xff1a;7-31 字符串循环左移 一. 题目 1. 题目 2. 输入输出样例 3. 限制 二、代码(python) 1. 代码实现 str1 input().split(\n)[0] num int(input()) len len(str1) if num > len:num num % len # 减少移动次数 print(str1[num:] str1[:num])2. 提交…

Spring Boot携手OAuth2.0,轻松实现微信扫码登录!

作者介绍&#xff1a;✌️大厂全栈码农|毕设实战开发&#xff0c;专注于大学生项目实战开发、讲解和毕业答疑辅导。 推荐订阅精彩专栏 &#x1f447;&#x1f3fb; 避免错过下次更新 Springboot项目精选实战案例 更多项目&#xff1a;CSDN主页YAML墨韵 学如逆水行舟&#xff0c…

21.基础乐理-等音调扩展篇、为何一共十五个大调

首先 等音调 的概念是基于 等音 的概念&#xff0c;比如下图中的音名&#xff1a;因为用的按键相同&#xff0c;音名不同&#xff0c;所以被称为等音调 然后音名一共有35个&#xff0c;如下图&#xff1a;所以在理论上它会有35个大调&#xff0c;但是人总是倾向于选择简单、简洁…

关于pdf.js中文本坐标尺寸的使用

一个电子教材项目中有这样一个需求&#xff1a; 用户向网站上传一个PDF书籍后&#xff0c;网站可以对PDF书籍进行解析&#xff0c;并支持用户对PDF书籍的每一页做一些操作&#xff0c;比如&#xff1a;为英语课本的单词和句子添加音频热区。因为热区数量很多&#xff0c;所以&a…

C# 图像处理 添加水印

方法1&#xff0c;使用自带的画刷进行绘制水印 示例代码 public partial class Form1 : Form{public Form1(){InitializeComponent();}string photoPathstring.Empty;Bitmap image null;private void button1_Click(object sender, EventArgs e) //选择照片{OpenFileDialog d…

LDA主题模型

在文本挖掘领域&#xff0c;大量的数据都是非结构化的&#xff0c;很难从信息中直接获取相关和期望的信息&#xff0c;一种文本挖掘的方法&#xff1a;主题模型&#xff08;Topic Model&#xff09;能够识别在文档里的主题&#xff0c;并且挖掘语料里隐藏信息&#xff0c;并且在…

Windows 下载、安装和使用 Postman 的详细教程!

Postman 是一个功能强大的API测试工具&#xff0c;它可以帮助程序员更轻松地测试和调试 API。在本文中&#xff0c;我们将讨论如何在 Windows 上安装和使用 Postman。 安装 Postman 首先&#xff0c;让我们从 Postman 的官方网站下载并安装&#xff1a;https://www.postman.c…

YOLOV5 TensorRT部署 BatchedNMS(engine模型推理)(下)

主要是在王新宇代码的基础上改进,引入对BatchedNMS的解码 文章目录 1. 修改yolov5.cpp2.修改yololayer.h1. 修改yolov5.cpp 首先增加全局变量,名字根据转onnx时修改的节点名字来,查看onnx文件可以看到,顺序不要弄错。 const char *INPUT_NAME = “images”; const char …

C语言——贪吃蛇游戏的实现

目录 一. 贪吃蛇的介绍 二. Win32 API 1. 控制台程序 2. COORD 控制台屏幕上的坐标 3. GetStdHandle 4. GetConsoleCursorInfo CONSOLE_CURSOR_INFO 5. SetConsoleCursorInfo 6. SetConsoleCursorPosition 封装的SetPos函数 7. GetAsyncKeyState 宏定义KEY_PRESS 三…
最新文章