当前位置:网站首页>The realization of the list
The realization of the list
2022-08-02 09:56:00 【Science52】
Before learning list we are going to have a look at the order of the table in front of the defect:
1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间.会有不小的消耗.
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费.例如当前容量为100,满了以后增容到 200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间.
It is these defects,So just can have linked list to deal with these problems,Next we are going to study is a singly linked list,Singly linked lists of defects also many,While waiting for the follow-up to two-way cycle lead the list will be able to better solve these problems
链表的概念及结构
概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表 中的指针链接次序实现的 .
The above is the list in the form of,There is a data to add a pointer for maintenance,防止数据的丢失.
链表的分类
1. 单向或者双向 2 . 带头或者不带头 3. 循环或者非循环
According to the permutation and combination we can know,总共有8种链表,当然,We only learn one of the most simple and most complex structure,Because it more specific;
1. 无头单向非循环链表:结构简单,一般不会单独用来存数据.实际中更多是作为其他数据结 构的子结构,如哈希桶、图的邻接表等等.另外这种结构在笔试面试中出现很多.
2. 带头双向循环链表:结构最复杂,一般用在单独存储数据.实际中使用的链表数据结构,都 是带头双向循环链表.另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带 来很多优势,实现反而简单了.
This time we are to achieve singly linked lists
链表的实现
先来看看接口
对于数据结构来说,Generally we basically seize the add or delete check to start learning.
First of all, starting from the print,We can print with a single traverse
void SLTNodePrint(SLTNode* phead)//打印
{
SLTNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
SLTNode* BuySLTNode(SLTDatatype x)//创建节点
{
SLTNode* tmp = (SLTNode*)malloc(sizeof(SLTNode));
if (tmp == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
tmp->data = x;
tmp->next = NULL;
}
return tmp;
}
头插头删尾插尾删:Here to preach the secondary pointer because we want to change a head node,We're going to pass to change the primary pointer secondary pointer,And print function does not change the head node,So don't need to pass the secondary pointer.
void SLTNodePushFront(SLTNode** pphead, SLTDatatype x)//头插
{
assert(pphead);
SLTNode* newnode = BuySLTNode(x);
if (*pphead == NULL)
{
*pphead = newnode;//With no data
}
else
{
//Processing data
newnode->next = *pphead;
*pphead = newnode;
}
}
void SLTNodePopFront(SLTNode** pphead)//头删
{
assert(pphead);
assert(*pphead);//Make sure that the head has data can delete
SLTNode* cur = *pphead;
*pphead = (*pphead)->next;
free(cur);
cur = NULL;//养成好习惯,freeSet to null pointer after
}
void SLTNodePushBack(SLTNode** pphead, SLTDatatype x)//尾插
{
assert(pphead);
SLTNode* newnode = BuySLTNode(x);
if (*pphead == NULL)
{
*pphead = newnode;//没有数据
}
else
{
SLTNode* cur = *pphead;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newnode;
}
}
void SLTNodePopBack(SLTNode** pphead)//尾删
{
assert(pphead);
assert(*pphead);//Prevent when no data is also delete
if ((*pphead)->next == NULL)//只有一个头节点
{
free((*pphead)->next);
*pphead= NULL;
}
else
{
//多个节点
SLTNode* cur = *pphead;
while (cur->next->next != NULL)
{
//Here to ensure that the address of a node to have before
//To delete the next node,And link after next node
cur = cur->next;
}
free(cur->next);
cur->next = NULL;
}
}
Below is a list of destruction:Because the list there is a link relationship,If there is no pointer to maintain,List, there is no way to link up again,导致数据的丢失.
void SLTNodeDestroy(SLTNode** pphead)//链表销毁
{
assert(pphead);
SLTNode* cur = *pphead;
while (cur)
{
SLTNode* next = cur->next;//记录下一个节点,防止找不到
free(cur);
cur = next;
}
*pphead = NULL;
}
查找:
SLTNode* SLTNodeFind(SLTNode* phead, SLTDatatype x)//查找
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
Modify the search is also realized,Section for any place to insert remove more convenient also:The following is achieved by looking for changes
void test3()
{
SLTNode* plist = NULL;
SLTNodePushBack(&plist, 1);
SLTNodePushBack(&plist, 2);
SLTNodePushBack(&plist, 3);
SLTNodePushBack(&plist, 4);
SLTNode* pos = SLTNodeFind(plist, 2);
if (pos)
{
pos->data *= 30;//Here is equivalent to implement the change data
printf("找到了%d\n",pos->data);
}
else
{
printf("找不到\n");
}
SLTNodePrint(plist);
}
Insert or delete data from the data front or more troublesome,But because the front insert demand is big,So will need to implement:
void SLTNodeInsert(SLTNode** pphead, SLTDatatype x,SLTNode* pos)
{
//从x的前面插入数据
assert(pphead);
SLTNode* newnode = BuySLTNode(x);
if (*pphead == pos)
{
SLTNodePushFront(pphead, x);//头插
}
else
{
SLTNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
assert(prev);//防止posThe wrong,Pass a null pointer,会导致死循环
}
newnode->next = prev->next;
prev->next = newnode;
}
}
void SLTNodeErase(SLTNode** pphead, SLTNode* pos)
{
//pos前删除
assert(pphead);
assert(*pphead);//To prevent delete empty
if (*pphead == pos)
{
SLTNodePopFront(pphead);//头删
}
else
{
SLTNode* prev = *pphead;
while (prev->next->next != pos)
{
prev = prev->next;
}
free(prev->next);
prev->next = pos;
}
}
从posAfter a position for insertion or deletion is simple,这里就不多赘述:直接上代码
void SLTNodeInsertAfter( SLTDatatype x, SLTNode* pos)
{
//在x的后面插入数据
SLTNode* newnode = BuySLTNode(x);
newnode->next = pos->next;//The two can't reverse,Otherwise can't link up
pos->next = newnode;
}
void SLTNodeEraseAfter(SLTNode* pos)
{
//pos之后删除
assert(pos->next);//Prevent after no one
SLTNode* tail = pos->next;
pos->next = tail->next;
free(tail);
tail = NULL;
}
In the final to singly linked lists are not suitable for storing data,Just as other data structures of substructure,但是,对于面试来说,单链表非常重要,Because for two-way cycle lead the list,Structure more perfect,Nothing is worth to take an examination of,So investigation singly linked lists more test of our ability.
边栏推荐
- The use of thread pool and analysis of ThreadPoolExecutor source code
- 干货|如何在海量文件系统中选择合适自己的文件系统
- 软件测试H模型
- CFdiv2-The Number of Imposters-(两种点集图上染色问题总结)
- 基于列表的排队与叫号系统
- 李航《统计学习方法》笔记之监督学习Supervised learning
- npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
- 练习-17
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- 牛客网项目2.7开发注册功能 报错This application has no explicit mapping for /error......
猜你喜欢
RPA助你玩转抖音,开启电商运营新引擎
leetcode:639. 解码方法 II
iNFTnews | 看见元宇宙的两面,何谓全真互联网和价值互联网?
php组件漏洞
斯皮尔曼相关系数
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Two-dimensional array piecemeal knowledge sorting
leetcode 62. Unique Paths(独特的路径)
node封装一个图片拼接插件
Navicat连接MySQL时弹出:1045:Access denied for user ‘root’@’localhost’
随机推荐
百战RHCE(第四十六战:运维工程师必会技-Ansible学习1-基础知识讲解)
js防抖函数和函数节流的应用场景
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
Rust 从入门到精通03-helloworld
让电商运营10倍提效的自动化工具,你get了吗?
R语言使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、设置theme主题参数自定义表格中表头内容的填充色(使用colnames.style参数)
每日一题练习1-15
单机部署flink,创建oracle19c rac的连接表时报错 ORA-12505 ,怎么回事?
剑指offer专项突击版第17天
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
HikariCP数据库连接池,太快了!
The 17th day of the special assault version of the sword offer
SAP 云平台上一种 Low Code Development(低代码开发)解决方案
php组件漏洞
稳定币:对冲基金做空 Tether 的结局会是什么?
The love-hate relationship between C language volatile keyword, inline assembly volatile and compiler
Nodejs3day(express简介,express创建基本Web服务器,托管静态资源,nodemon下载及出现的问题,中间件,编写GET,POST,JSONP接口)
重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透
使用scrapy 把爬到的数据保存到mysql 防止重复
百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)