当前位置:网站首页>【双链表增删查改接口的实现】
【双链表增删查改接口的实现】
2022-08-09 21:35:00 【努力上进呀】
hello 大家好呀,今天向大家分享的是双链表增删查改接口的实现,如果哪里有什么不对的地方希望各位佬帮忙指正一下。
这次博主分享的是带头双向循环链表:
目录
1 初始化链表
ListNode* ListNodeCreat(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode)
{
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
}
return newnode;
}
ListNode* InitList(void)
{
ListNode* phead = ListNodeCreat(1);
phead->next = phead;
phead->prev = phead;
return phead;
}
链表的初始化我们将phead的prev与next都指向phead,这里的处理在后面有一些妙用,这里就先不说了,头结点这里是通过返回值实现的,当然,通过二级指针也可以,效果都差不多。头结点是不存储数据的,只是充当哨兵的作用。
2 尾插和头插
尾插的具体代码:
void ListPushBack(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newtail = ListNodeCreat(x);
tail->next = newtail;
newtail->prev = tail;
newtail->next = phead;
phead->prev = newtail;
//ListInsert(phead, x);
}
尾插的实现很简单,都能够看懂。在这里我们就可以看见将phead的prev与next都指向phead的好处了,就算phead后面一个结点都没有依然能够处理,这样写代码就很简练了。
头插的具体代码:
void ListPushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* next = phead->next;
ListNode* newnode = ListNodeCreat(x);
phead->next = newnode;
newnode->prev = phead;
newnode->next = next;
next->prev = newnode;
//ListInsert(phead->next, x);
}
头插的实现也比较简单,其实把图画好就能够解决问题,这里当头结点后面一个结点都没有也能够处理。
来看看效果:
3 尾删和头删
尾删和头删对于双链表来说也比较简单,这里就放在一起写了:
void ListPopBack(ListNode* phead)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
//ListErase(phead->prev);
}
void ListPopFront(ListNode* phead)
{
assert(phead);
ListNode* next = phead->next->next;
free(phead->next);
phead->next = next;
next->prev = phead;
//ListErase(phead->next);
}
按部就班的写,注意不要写漏了就ok了。
效果:
4 查找和查插和查删)
查找的具体代码:
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}
与单链表一样,这个也可以改变数据:
查插的具体代码:
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* newnode = ListNodeCreat(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
这里是在pos位前面插入数据,实现起来也比较简单。
查删的具体代码:
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
有了查删和查插后我们不难发现,头插和尾插都可以用查插实现,头删和尾删都可以用查删实现。
头插在 phead->next前插入,尾插在phead前插入,头删位置为phead->next,尾删位置为phead->prev.
在上面实现代码的时候注释掉的就是这种方法。
5 释放
void ListDestroy(ListNode* phead)
{
ListNode* cur = phead;
ListNode* next = phead->next;
while (cur->next!=phead)
{
free(cur);
cur = next;
next = next->next;
}
}
释放链表与单链表差不多,比较容易理解。
好了,今天的分享就到这里了,如果该文对你有帮助的话能不能3连支持一下博主呢?
边栏推荐
- UE4_定序器控制蓝图对象
- PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
- [Graphic and textual] How to reinstall Win7 system
- windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
- PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
- Access control knowledge
- Overview of Security Analysis Technology for Smart Home Devices
- 微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
- Photometric Stereo 光度立体法三维重建
- 【云原生】4.2 DevOps 精讲篇
猜你喜欢
如何让您的公司内容满足 GDPR 合规性
CMake installation upgrade higher version
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
一千以内的水仙花数
Cholesterol-PEG-Thiol,CLS-PEG-SH,胆固醇-聚乙二醇-巯基用于改善溶解度
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
LoRa无线技术在物联网应用市场的概况和发展
windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
[corctf 2022] 部分
Can I make a TCP connection without accept?
随机推荐
定投的基金
SecureCRT背景配色
mysql多表左链接查询
DSPE-PEG-Azide,DSPE-PEG-N3,磷脂-聚乙二醇-叠氮可和DBCO直接反应
[Generic Programming] Full Detailed Explanation of Templates
FS4066耐高压1到4节内置MOS的锂电池充电管理芯片
[Essay] To the friends of the 19th issue
哪款C语言编译器(IDE)适合初学者?
【随笔】致19期的小伙伴们
Error when source install/setup.bash
Endpoint mode for NetCore routing
必看设计干货|易知微设计师是怎么做标准可视化设计服务的?
CMake installation upgrade higher version
Referenced file contains errors 完美解决方法
消防安全培训|“蓝朋友”,开课了!
6 g underwater channel modeling were summarized based on optical communication
字符串哈希(2014 SERC J题)
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
Jmeter 使用正则表达式提取器将返回值全部保存到一个文件中
leetcode: the Kth largest element in the array