• 【数据结构与算法】链表


    链表的引入

    对于顺序表存在一些缺陷:

    • 中间/头部的插入删除,时间复杂度为O(N) 。头部插入需要挪动后面的元素

    • 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。

    • 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间

    对于链表而言,能够避免上述问题的出现。头部插入数据不需要挪动大量的数据,按需申请释放空间,不会造成空间的浪费。

    查看源图像

    链表的概念及结构

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

    image-20220729143606839

    现实中 数据结构中(箭头实际上并不存在,这里只是形象化):

    image-20220729144054431

    链表的种类有很多

    以下情况组合起来就有8种链表结构:

    单向或者双向 :

    带头或者不带头:image-20220729144300035

    循环或者非循环:image-20220729144319400

    面对这么多种类的链表,我们该如何选择?虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:

    image-20220729144358873

    1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等
    2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了

    话不多说,直接进入我们单链表的实现:

    单链表的接口我们需要实现:

    • 打印销毁

    • 头插尾插创建新结点(由于后面会频繁使用,故将其封装成函数)

    • 尾删头删查找

    • 在pos之前插入、在pos之后插入

    • 删除pos、删除pos之后的位置

    我们还是老样子,通过三个部分组成:

    **SList.h:**包括头文件的引用,结构体的声明定义,接口函数的声明。

    **SList.c:**对SList.h中接口函数进行实现。

    **test.c:**主函数进行测试:通过void TestSList()函数对SList.c的函数进行调用,测试有没有错误。

    注意:对于一些问题的所在,我已经通过注释进行相关说明。注释才是精髓。

    SList.h

    #define _CRT_SECURE_NO_WARNINGS
    
    #pragma once
    #include 
    #include 
    #include 
    
    typedef int SLTDataType;
    typedef struct SListNode
    {
    	SLTDataType data;
    	struct SLTNode* next;
    }SLTNode;
    
    //打印
    void SListPrint(SLTNode* phead);
    
    //创建新结点
    SLTNode* BuySLTNode(SLTDataType x);
    
    //销毁
    void SListDestory(SLTNode** pphead);
    
    //头插
    void SListPushFront(SLTNode** pphead, SLTDataType x);
    
    //尾插
    void SListPushBack(SLTNode** pphead, SLTDataType x);
    
    //尾删
    void SListPopBack(SLTNode**pphead);
    
    //头删
    void SListPopFront(SLTNode**pphead);
    
    //查找
    SLTNode* SListFind(SLTNode* pphead,SLTDataType x);
    
    //在pos之前插入
    void SListInsert(SLTNode** pphead,SLTNode*pos,SLTDataType x);
    
    //在pos之后插入
    void SListInsertAfter(SLTNode* pos, SLTDataType x);
    
    //删除pos
    void SListErase(SLTNode** pphead,SLTNode*pos);
    
    
    //删除pos后面位置,了解
    void SListEraseAfter(SLTNode* pos);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    image-20220731144234241

    SList.c

    #define _CRT_SECURE_NO_WARNINGS
    
    #include "SList.h"
    //打印
    void SListPrint(SLTNode* phead)
    {
    	//phead不需要断言。因为phead有可能有空,没有数据
    	//而顺序表的结构体不可能为空,所以要进行断言
    	SLTNode* cur = phead;
    	while (cur != NULL)
    	{
    		printf("%d->", cur->data);
    		cur = cur->next;
    	}
    	printf("NULL\n");
    
    
    }
    
    //创建新结点
    SLTNode* BuySLTNode(SLTDataType x)
    {
    	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    	if (newnode == NULL)
    	{
    		perror("mail fail");
    		exit(-1);
    	}
    	newnode->data = x;
    	newnode->next = NULL;
    	return newnode;
    }
    
    //销毁
    void SListDestory(SLTNode** pphead)
    {
    	assert(pphead);
    	SLTNode* cur = *pphead;
    	while (cur)
    	{
    		SLTNode* next = cur->next;
    		free(cur);
    		cur = next;
    	}
    	*pphead = NULL;
    }
    
    //头插
    void SListPushFront(SLTNode** pphead, SLTDataType x)
    {
    	assert(pphead);
    	SLTNode* newnode = BuySLTNode(x);
    	newnode->next = *pphead;
    	*pphead = newnode;
    }
    
    
    //尾插——有无结点。需要找前一个
    void SListPushBack(SLTNode** pphead, SLTDataType x)
    {
    	assert(pphead);
    	SLTNode* newnode = BuySLTNode(x);
    
    	//空,改变的是SListNode*,需要二级指针
    
    	//非空。尾插要改变的是结构体SListNode,只需要结构体的指针
    
    	if (*pphead == NULL)
    	{
    		*pphead = newnode;
    	}
    	else
    	{
    		//找尾
    		SLTNode* tail = *pphead;
    		while (tail->next != NULL)
    		{
    			tail = tail->next;
    		}
    		tail->next = newnode;
    	}
    }
    
    //尾删——有无结点。需要找前一个
    void SListPopBack(SLTNode** pphead)
    {
    	assert(pphead);
    	SLTNode* tail = *pphead;
    	SLTNode* prev = NULL;
    	assert(*pphead != NULL);
    	if ((*pphead)->next == NULL)
    	{
    		free(*pphead);
    		*pphead = NULL;
    	}
    	else
    	{
    		while (tail->next != NULL)
    		{
    			prev = tail;
    			tail = tail->next;
    		}
    		prev->next = NULL;
    		free(tail);
    		tail = NULL;
    		/*while (tail->next->next!=NULL)
    		{
    			tail = tail->next;
    		}
    		free(tail->next);
    		tail->next = NULL;*/
    
    	}
    }
    
    //头删
    void SListPopFront(SLTNode** pphead)
    {
    	assert(pphead);
    	assert(*pphead);
    	/*if (*pphead == NULL)
    	{
    		return;
    	}*/
    	SLTNode* del = *pphead;
    	*pphead = (*pphead)->next;
    	free(del);
    	del = NULL;
    }
    
    //查找
    SLTNode* SListFind(SLTNode* pphead, SLTDataType x)
    {
    	assert(pphead);
    	SLTNode* cur = pphead;
    	while (cur)
    	{
    		if (cur->data == x)
    		{
    			return cur;
    		}
    		cur = cur->next;
    	}
    	return NULL;
    }
    
    
    //在pos之前插入,需要找前一个
    void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
    {
    	assert(pphead);
    	assert(pos);
    	if (pos == *pphead)
    	{
    		SListPushFront(pphead, x);
    	}
    	else
    	{
    		SLTNode* newnode = BuySLTNode(x);
    		SLTNode* prev = *pphead;
    		while (prev->next != pos)
    		{
    			prev = prev->next;
    			assert(prev);
    		}
    		prev->next = newnode;
    		newnode->next = pos;
    	}
    }
    
    
    //在pos后插入
    void SListInsertAfter(SLTNode* pos, SLTDataType x)
    {
    	assert(pos);
    	SLTNode* newnode = BuySLTNode(x);
    	newnode->next = pos->next;
    	pos->next = newnode;
    }
    
    
    //删除pos位置,需要找前一个
    void SListErase(SLTNode** pphead, SLTNode* pos)
    {
    	assert(pphead);
    	assert(pos);
    	if (pos == *pphead)
    	{
    		SListPopFront(pphead);
    	}
    	else
    	{
    		SLTNode* prev = *pphead;
    		while (prev->next != pos)
    		{
    			prev = prev->next;
    			//检查pos不是链表中的结点
    			assert(prev);
    		}
    		prev->next = pos->next;
    		free(pos);
    	}
    }
    
    
    //删除pos后面位置
    void SListEraseAfter(SLTNode* pos)
    {
    	assert(pos);
    	if (pos->next == NULL)
    	{
    		return;
    	}
    	else
    	{
    		SLTNode* next = pos->next;
    		pos->next = next->next;
    		free(next);
    
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221

    img

    test.c

    #define _CRT_SECURE_NO_WARNINGS
    
    #include "SList.h"
    
    
    //头插、头删
    void TestSList1()
    {
    	SLTNode* plist = NULL;
    	SListPushFront(&plist, 1);
    	SListPushFront(&plist, 2);
    	SListPushFront(&plist, 3);
    	SListPushFront(&plist, 4);
    	SListPrint(plist);
    
    	SListPopFront(&plist);
    	SListPrint(plist);
    	SListPopFront(&plist);
    	SListPrint(plist);
    	SListPopFront(&plist);
    	SListPrint(plist);
    	SListPopFront(&plist);
    	SListPrint(plist);
    	SListPopFront(&plist);
    	SListPrint(plist);
    
    	SListDestory(&plist);
    
    }
    
    //尾插、尾删
    void TestSList2()
    {
    	SLTNode* plist = NULL;
    	SListPushBack(&plist, 1);
    	SListPushBack(&plist, 2);
    	SListPushBack(&plist, 3);
    	SListPushBack(&plist, 4);
    	SListPrint(plist);
    
    	SListPopBack(&plist);
    	SListPopBack(&plist);
    	SListPopBack(&plist);
    	SListPopBack(&plist);
    	SListPopBack(&plist);
    
    	SListDestory(&plist);
    }
    
    
    //查找、在pos之前插入
    void TestSList3()
    {
    	SLTNode* plist = NULL;
    	SListPushBack(&plist, 1);
    	SListPushBack(&plist, 2);
    	SListPushBack(&plist, 3);
    	SListPushBack(&plist, 4);
    	SListPrint(plist);
    
    	SLTNode* pos = SListFind(plist, 3);
    	if (pos)
    	{
    		//修改
    		pos->data *= 10;
    		printf("找到了\n");
    	}
    	else
    	{
    		printf("找不到\n");
    	}
    
    	pos = SListFind(plist, 1);
    	if (pos)
    	{
    		SListInsert(&plist, pos, 10);
    	}
    
    
    	SListPrint(plist);
    	SListDestory(&plist);
    
    }
    
    //删除pos位置、删除pos后面的位置
    void TestSList4()
    {
    
    	SLTNode* plist = NULL;
    	SListPushBack(&plist, 1);
    	SListPushBack(&plist, 2);
    	SListPushBack(&plist, 3);
    	SListPushBack(&plist, 4);
    	SListPrint(plist);
    
    	SLTNode* pos = SListFind(plist, 3);
    	if (pos)
    	{
    		SListErase(&plist, pos);
    	}
    	SListPrint(plist);
    
    	pos = SListFind(plist, 1);
    	if (pos)
    	{
    		SListErase(&plist, pos);
    	}
    	SListPrint(plist);
    }
    
    
    int main()
    {
    	//TestSList1();
    	//TestSList2();
    	//TestSList3();
    	TestSList4();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119

    image-20220731144450245


    以上就是单链表的相关操作,我们不难发现,单链表的优势在于头插头删

    而对于一些操作:如尾插尾删而言,我们都需要去找前一个位置,这是比较麻烦的。单链表我们就先介绍到这里了。

    这里同时也有一个问题存在:

    删除当前位置我们需要去找前一个位置,这效率是比较低的,我们如何改进这个问题❓

    找pos位置删除,而就是不找前一个位置(即要求是O(1)):替换法删除,把pos的值和下一个节点的值进行交换,再把pos进行释放掉。但是有一个缺陷:pos不能是尾节点。尾节点没有下一项。

    那如果在pos位置之前插入,要求为O(1)呢:

    直接插入到pos后面,然后进行交换。


    总结

    • 我们从链表的引入开始,了解了为什么链表会出现,同时认识了链表的概念和结构。本篇博客主要介绍了单链表的操作,以及解决了一个问题。本次就先到这里结束了。

  • 相关阅读:
    [ROS](09)ROS通信 —— 话题(Topic)通信之msg
    二十八、CANdelaStudio实践-10服务(SessionControl)
    51单片机-LED实验二
    【rar密码】如何修改rar压缩包密码?
    每日五问(java)
    前端进击笔记第二十五节 通过前端工程化提升团队开发效率
    C++——类与对象(上)
    PyTorch深度学习遥感影像地物分类与目标检测、分割及遥感影像问题深度学习优化实践技术应用
    计算机毕业设计选题推荐-房屋租赁系统-Java/Python项目实战
    前端工作方式要换了?HTMX简介:无需JavaScript的动态HTML
  • 原文地址:https://blog.csdn.net/weixin_60478154/article/details/126086370