数据结构单链表

news/2025/3/14 1:04:31/

在这里插入图片描述

单链表
1 链表的概念及结构
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链
接次序实现的 。

在我们开始讲链表之前,我们是写了顺序表,顺序表就是类似一个数组的东西,它的存放是连续的,优点有很多,比如支持我们随机访问,连续存放,命中率高,区别于单链表我们可以用类似数组的下标进行访问,这大大的提高我们的效率,但是也有缺点,空间不够就要需要扩容,扩容存在消耗的,头部或者中间位置的插入删除,需要挪动,挪动数据也是存在消耗的。避免频繁扩容,一次一般都是按倍数扩容,可能存在空间扩容。

链表的优点:
按需申请空间,不用释放空间。
头部或者中间位置的插入和删除,不需要挪动数据。
不存在空间浪费。

链表的缺陷:
每一个数据,都要存放一个指针去链表后面节点的地址。
不支持随机访问。

链表的结构

typedef int SLNodedataType;
typedef struct SList
{SLNodedataType data;struct SList* next;}SLNode;

这个就是我们单链表的基本代码,我们来用图更加清清楚的表示一下它完整的样子。
在这里插入图片描述
这就我们基本的逻辑结构,它前一个的next是存放后面的地址的,这样就能找到我们下一个节点。

单链表使用的时候相比和顺序表比较的话,它的使用不会浪费空间,我们需要一个节点就可以开辟一个节点出来供我们使用。但是它存储就不是连续的了。

那我们现在开始写代码来实现单链表。
单链表
首先我们要创建一个结构体。

typedef int SLNodedataType;
typedef struct SList
{SLNodedataType data;struct SList* next;}SLNode;

接下来我们首先要打印我们的单链表
在这之前我们应该创建节点,创捷节点很简单,就是按照我们上面的图的前一个存放后面的地址。

//创建节点SLNode* n1 = (SLNode*)malloc(sizeof(SLNode));assert(n1);SLNode* n2 = (SLNode*)malloc(sizeof(SLNode));assert(n2);SLNode* n3 = (SLNode*)malloc(sizeof(SLNode));assert(n3);SLNode* n4 = (SLNode*)malloc(sizeof(SLNode));assert(n4);n1->data = 1;n2->data = 2;n3->data = 3;n4->data = 4;n1->next = n2;n2->next = n3;n3->next = n4;n4->next = NULL;

那下面就是我们的打印单链表。

void SListPrint(SLNode* plist)
{SLNode* cur = plist;while (cur != NULL){printf("%d->", cur->data);cur = cur->next;}printf("NULL");printf("\n");}

我们来测试一下看看效果。

在这里插入图片描述
可以看到我们的单链表也是成功的打印,那接下来就是要写出我们的尾插函数。
写之前我们先来分析分析,首先尾插一个节点进去,那我们是不是要有一个这样的节点,竟然这样就可以写一个创造节点的函数。就叫他CreateSListNode

SLNode* CreateSListNode(SLNodedataType x)
{SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));newnode->data = x;newnode->next = NULL;return newnode;
}

写完这个那我们写一个尾插函数,尾插的时候我们要想一想要传什么地址过去,如果是有数据的话其实我们传一级地址就行,但是如果是空的话,就得传二级,因为我们要改变plist的位置。但是也其实是相当于头插,没节点的时候,总不能在空指针后面插入。那我们写一个 吧。

void SListPushBcak(SLNode** plist, SLNodedataType x)
{SLNode*newnode=CreateSListNode(x);assert(plist);if (*plist == NULL){plist = newnode;}else{SLNode* tail = *plist;while (tail->next != NULL){tail = tail->next;}tail->next = newnode;}}

看一下我们编译的结果

在这里插入图片描述
最后也是成功的尾插进去,那尾插之后就应该要写一个尾删。
写尾删的时候,我们要先考虑怎么找到最后,这和尾插一样,遍历一遍找到最后一个,然后free掉就行了。

在这里插入图片描述
代码

void SListPopBack(SLNode** plist)
{SLNode* tail = *plist;SLNode* prev = NULL;while (tail->next != NULL){prev = tail;tail = tail->next;}free(tail);prev->next = NULL;
}

这其实就是用了一个双指针的方法找最后一个的前一个,但是我们还需要注意链表不能为空,空了怎么删除啊。所以改进一下。

void SListPopBack(SLNode** plist)
{assert(plist);assert(*plist);SLNode* tail = *plist;SLNode* prev = NULL;while (tail->next != NULL){prev = tail;tail = tail->next;}free(tail);prev->next = NULL;
}
void test1()
{//创建节点SLNode* n1 = (SLNode*)malloc(sizeof(SLNode));assert(n1);SLNode* n2 = (SLNode*)malloc(sizeof(SLNode));assert(n2);SLNode* n3 = (SLNode*)malloc(sizeof(SLNode));assert(n3);SLNode* n4 = (SLNode*)malloc(sizeof(SLNode));assert(n4);n1->data = 1;n2->data = 2;n3->data = 3;n4->data = 4;n1->next = n2;n2->next = n3;n3->next = n4;n4->next = NULL;SListPrint(n1);SListPushBcak(&n1, 5);SListPushBcak(&n1, 6);SListPushBcak(&n1, 7);SListPushBcak(&n1, 8);SListPrint(n1);SListPopBack(&n1);SListPopBack(&n1);SListPrint(n1);
}

不过其实我们也可以不用双指针的办法。
那也整一个玩玩吧


void SListPopBack(SLNode** plist)
{assert(plist);assert(*plist);SLNode* tail = *plist;while (tail->next->next != NULL){tail = tail->next;}free(tail->next);tail->next = NULL;

其实道理是一样的,就是找下下一个的节点是不是为空。
尾插写好就是头插,来吧展示。

void SListPushFront(SLNode** plist, SLNodedataType x)
{assert(plist);SLNode* newnode = CreateSListNode(x);if (*plist == NULL){*plist = newnode;}else{newnode->next = *plist;*plist = newnode;}}

其实想明白也不难,接下来就是头删。

void test1()
{//创建节点SLNode* n1 = (SLNode*)malloc(sizeof(SLNode));assert(n1);SLNode* n2 = (SLNode*)malloc(sizeof(SLNode));assert(n2);SLNode* n3 = (SLNode*)malloc(sizeof(SLNode));assert(n3);SLNode* n4 = (SLNode*)malloc(sizeof(SLNode));assert(n4);n1->data = 1;n2->data = 2;n3->data = 3;n4->data = 4;n1->next = n2;n2->next = n3;n3->next = n4;n4->next = NULL;SListPrint(n1);SListPushBcak(&n1, 5);SListPushBcak(&n1, 6);SListPushBcak(&n1, 7);SListPushBcak(&n1, 8);SListPrint(n1);SListPopBack(&n1);SListPopBack(&n1);SListPrint(n1);SListPushFront(&n1, 111);SListPushFront(&n1, 222);SListPrint(n1);SListPopFront(&n1);SListPopFront(&n1);SListPopFront(&n1);SListPrint(n1);}
void SListPopFront(SLNode** plist)
{assert(plist);assert(*plist);SLNode* cur = (*plist)->next;free(*plist);*plist = cur;
}

我们在写一个查找功能的代码

SLNode* SLFind(SLNode* plist, SLNodedataType x);

查找我们可以返回这个节点,这样就能和其他功能一起用,比如修改数据,或者在任意位置插入和删除。

SLNode* SLFind(SLNode* plist, SLNodedataType x)
{SLNode* pos = plist;while (pos->data == x){return pos;pos = pos->next;}
}

这是只考虑找到的情况下,但是难免有时候会出现找不到的情况,让我们来看一下吧,写一个找不到情况下和找到情况下的代码。‘

SLNode* SLFind(SLNode* plist, SLNodedataType x)
{SLNode* pos = plist;while (pos != NULL){if (pos->data == x){return pos;}pos = pos->next;}return NULL;
}

然后我们可以写一个函数来判断有没有找到。

SLNode*pos = SLFind(n1, 111);if (pos != NULL){printf("找到了\n");}else{printf("找不到\n");}

我们看完整代码。

void test1()
{//创建节点SLNode* n1 = (SLNode*)malloc(sizeof(SLNode));assert(n1);SLNode* n2 = (SLNode*)malloc(sizeof(SLNode));assert(n2);SLNode* n3 = (SLNode*)malloc(sizeof(SLNode));assert(n3);SLNode* n4 = (SLNode*)malloc(sizeof(SLNode));assert(n4);n1->data = 1;n2->data = 2;n3->data = 3;n4->data = 4;n1->next = n2;n2->next = n3;n3->next = n4;n4->next = NULL;SListPrint(n1);SListPushBcak(&n1, 5);SListPushBcak(&n1, 6);SListPushBcak(&n1, 7);SListPushBcak(&n1, 8);SListPrint(n1);SListPopBack(&n1);SListPopBack(&n1);SListPrint(n1);SListPushFront(&n1, 111);SListPushFront(&n1, 222);SListPrint(n1);SListPopFront(&n1);SListPopFront(&n1);SListPopFront(&n1);SListPrint(n1);SLNode*pos = SLFind(n1, 111);if (pos != NULL){printf("找到了\n");}else{printf("找不到\n");}}

我们如果要找111发现没有找到,因为头删的时候改掉,其实我们竟然这样写了就可以写一个修改的代码,这里就不演示了。
接下来我们要写的是在任意位置删除和插入节点。

void SListPushInsert(SLNode** plist, SLNode* pos, SLNodedataType x)
{assert(plist);assert(pos);SLNode* newnode = CreateSListNode(x);if (pos == *plist){SListPushFront(plist, x);}else{SLNode* prev = *plist;while (prev->next != pos){prev = prev->next;}prev->next = newnode;newnode->next = pos;}
}

测试代码

void test1()
{//创建节点SLNode* n1 = (SLNode*)malloc(sizeof(SLNode));assert(n1);SLNode* n2 = (SLNode*)malloc(sizeof(SLNode));assert(n2);SLNode* n3 = (SLNode*)malloc(sizeof(SLNode));assert(n3);SLNode* n4 = (SLNode*)malloc(sizeof(SLNode));assert(n4);n1->data = 1;n2->data = 2;n3->data = 3;n4->data = 4;n1->next = n2;n2->next = n3;n3->next = n4;n4->next = NULL;SListPrint(n1);SListPushBcak(&n1, 5);SListPushBcak(&n1, 6);SListPushBcak(&n1, 7);SListPushBcak(&n1, 8);SListPrint(n1);SListPopBack(&n1);SListPopBack(&n1);SListPrint(n1);SListPushFront(&n1, 111);SListPushFront(&n1, 222);SListPrint(n1);SListPopFront(&n1);SListPopFront(&n1);SListPopFront(&n1);SListPrint(n1);SLNode*pos = SLFind(n1,3);if (pos != NULL){printf("找到了\n");SListPushInsert(&n1, pos, 10086);}else{printf("找不到\n");}SListPrint(n1);
}

在任意位置删除

void SListPopInsert(SLNode** plist, SLNode* pos)
{assert(plist);assert(*plist);assert(pos);if (*plist == pos){SListPopFront(plist);}else{SLNode* prev = *plist;while (prev->next != pos){prev = prev->next;}prev->next = pos->next;free(pos);}
}

其实还有可以在任意位置后删除,这样更快,就不用找那个位置前一个位置了,这里就不展示了,
完整代码

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<assert.h>
#include<stdlib.h>typedef int SLNodedataType;
typedef struct SList
{SLNodedataType data;struct SList* next;}SLNode;void SListPrint(SLNode* plist);SLNode* CreateSListNode(SLNodedataType x);void SListPushBcak(SLNode** plist, SLNodedataType x);void SListPopBack(SLNode** plist);void SListPushFront(SLNode** plist, SLNodedataType x);void SListPopFront(SLNode** plist);SLNode* SLFind(SLNode* plist, SLNodedataType x);void SListPushInsert(SLNode** plist, SLNode* pos, SLNodedataType x);void SListPopInsert(SLNode** plist, SLNode* pos);
#include"SList.h"void SListPrint(SLNode* plist)
{SLNode* cur = plist;while (cur != NULL){printf("%d->", cur->data);cur = cur->next;}printf("NULL");printf("\n");}SLNode* CreateSListNode(SLNodedataType x)
{SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));newnode->data = x;newnode->next = NULL;return newnode;
}void SListPushBcak(SLNode** plist, SLNodedataType x)
{SLNode*newnode=CreateSListNode(x);assert(plist);if (*plist == NULL){plist = newnode;}else{SLNode* tail = *plist;while (tail->next != NULL){tail = tail->next;}tail->next = newnode;}}void SListPopBack(SLNode** plist)
{assert(plist);assert(*plist);SLNode* tail = *plist;SLNode* prev = NULL;while (tail->next != NULL){prev = tail;tail = tail->next;}free(tail);prev->next = NULL;
}
//
//void SListPopBack(SLNode** plist)
//{
//	assert(plist);
//	assert(*plist);
//	SLNode* tail = *plist;
//	
//	while (tail->next->next != NULL)
//	{
//		
//		tail = tail->next;
//	}
//	free(tail->next);
//	tail->next = NULL;
//	
//}void SListPushFront(SLNode** plist, SLNodedataType x)
{assert(plist);SLNode* newnode = CreateSListNode(x);if (*plist == NULL){*plist = newnode;}else{newnode->next = *plist;*plist = newnode;}}void SListPopFront(SLNode** plist)
{assert(plist);assert(*plist);SLNode* cur = (*plist)->next;free(*plist);*plist = cur;
}//SLNode* SLFind(SLNode* plist, SLNodedataType x)
//{
//	SLNode* pos = plist;
//	while (pos->data == x)
//	{
//		return pos;
//		pos = pos->next;
//	}
//}SLNode* SLFind(SLNode* plist, SLNodedataType x)
{SLNode* pos = plist;while (pos != NULL){if (pos->data == x){return pos;}pos = pos->next;}return NULL;
}void SListPushInsert(SLNode** plist, SLNode* pos, SLNodedataType x)
{assert(plist);assert(pos);SLNode* newnode = CreateSListNode(x);if (pos == *plist){SListPushFront(plist, x);}else{SLNode* prev = *plist;while (prev->next != pos){prev = prev->next;}prev->next = newnode;newnode->next = pos;}
}void SListPopInsert(SLNode** plist, SLNode* pos)
{assert(plist);assert(*plist);assert(pos);if (*plist == pos){SListPopFront(plist);}else{SLNode* prev = *plist;while (prev->next != pos){prev = prev->next;}prev->next = pos->next;free(pos);}
}

测试主函数的也发一下吧,大家可以不用放一起测试,有点看不过来。

#include"SList.h"void test1()
{//创建节点SLNode* n1 = (SLNode*)malloc(sizeof(SLNode));assert(n1);SLNode* n2 = (SLNode*)malloc(sizeof(SLNode));assert(n2);SLNode* n3 = (SLNode*)malloc(sizeof(SLNode));assert(n3);SLNode* n4 = (SLNode*)malloc(sizeof(SLNode));assert(n4);n1->data = 1;n2->data = 2;n3->data = 3;n4->data = 4;n1->next = n2;n2->next = n3;n3->next = n4;n4->next = NULL;SListPrint(n1);SListPushBcak(&n1, 5);SListPushBcak(&n1, 6);SListPushBcak(&n1, 7);SListPushBcak(&n1, 8);SListPrint(n1);SListPopBack(&n1);SListPopBack(&n1);SListPrint(n1);SListPushFront(&n1, 111);SListPushFront(&n1, 222);SListPrint(n1);SListPopFront(&n1);SListPopFront(&n1);SListPopFront(&n1);SListPrint(n1);SLNode*pos = SLFind(n1,3);if (pos != NULL){printf("找到了\n");SListPushInsert(&n1, pos, 10086);}else{printf("找不到\n");}SListPrint(n1);
}
int main()
{test1();return 0;
}

今天的分享就到这里,我们下次再见。


http://www.ppmy.cn/news/1039279.html

相关文章

手撕单链表

目录 链表的概念和结构 单链表的实现 申请新结点 打印 尾插 头插 尾删 头删 ​编辑 查找 在pos位置前插入元素 在pos位置后插入元素 删除pos位置的元素 删除pos位置之后的位置的元素​编辑 完整代码 SListNode.h SListNode.c 链表的概念和结构 链表是一种物理存储…

C++新经典03--共用体、枚举类型与typedef

共用体 共用体&#xff0c;也叫联合&#xff0c;有时候需要把几种不同类型的变量存放到同一段内存单元&#xff0c;例如&#xff0c;把一个整型变量、一个字符型变量、一个字符数组放在同一个地址开始的内存单元中。这三个变量在内存中占的字节数不同&#xff0c;但它们都从同…

【Go 基础篇】Go语言基本数据类型转换:字符串、整数、浮点数、字符与布尔类型的转换

介绍 在计算机编程中&#xff0c;不同的数据类型用于表示不同种类的数据。在Go语言&#xff08;Golang&#xff09;中&#xff0c;基本数据类型包括字符串、整数、浮点数、字符和布尔类型。在实际开发中&#xff0c;经常需要进行不同数据类型之间的转换&#xff0c;以满足不同…

第九章 动态规划part10(代码随想录)

121. 买卖股票的最佳时机 1. 确定dp数组&#xff08;dp table&#xff09;以及下标的含义 用二维dp数组表示第i天的2种状态 dp[i][0] 表示第i天持有股票所得最多现金&#xff0c;可能i-1天就买股票了 dp[i][1] 表示第i天不持有股票所得最多现金 最后求&#xff1a;dp[len-1][0…

干翻Dubbo系列第十一篇:Dubbo常见协议与通信效率对比

文章目录 文章说明 一&#xff1a;协议 1&#xff1a;什么是协议 2&#xff1a;协议和序列化关系 3&#xff1a;协议组成 &#xff08;一&#xff09;&#xff1a;头信息 &#xff08;二&#xff09;&#xff1a;体信息 4&#xff1a;Dubbo3中常见的协议 5&#xff1a;…

vue5种模糊查询方式

在Vue中&#xff0c;有多种方式可以实现模糊查询。以下是五种常见的模糊查询方式&#xff1a; 使用JavaScript的filter()方法&#xff1a;使用filter()方法可以对数组进行筛选&#xff0c;根据指定的条件进行模糊查询。例如&#xff1a; data() {return {items: [{ name: App…

Android EditText 使用(详细版)

经典好文推荐,通过阅读本文,您将收获以下知识点: 一、EditText 继承关系 二、EditText 常用举例 三、EditText 自定义背景框 四、EditText自动检测输入内容 五、Edittext 密文显示 六、EditText 限制只能输入特定字符 七、EditText 输入保存的字符串不能为空 一、EditText 继…

JWT(JSON Web Token )令牌

1、介绍 jwt就是将原始的json数据格式进行了安全的封装&#xff0c;这样就可以直接基于jwt在通信双方安全的进行信息传输了。 2、jwt组成 第一部分&#xff1a;Header(头&#xff09;&#xff0c; 记录令牌类型、签名算法等。 例如&#xff1a;{"alg":"HS256…