当前位置:网站首页>[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
2022-08-09 23:13:00 【work hard】
hello 大家好呀,Today I will share with you the implementation of the double-linked list addition, deletion, search and modification interface,If there is anything wrong, I hope you guys can help me correct it.
This time, the blogger shared the bidirectional circular linked list:

目录
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;
}The initialization of the linked list we willphead的prev与next都指向phead,The processing here has some magical uses in the back,这里就先不说了,The header node is implemented here by returning a value,当然,It is also possible to pass a secondary pointer,效果都差不多.The head node does not store data,Just act as a sentinel.
2 尾插和头插
The specific code of the tail insertion:
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);
}The implementation of the tail plug is very simple,can understand.Here we can see willphead的prev与next都指向phead的好处了,就算pheadNone of the latter nodes can still be processed,This makes writing code very concise.
The specific code of the header:
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);
}The implementation of the header plug is also relatively simple,In fact, a good picture can solve the problem,Here, there is no node behind the head node and it can also be processed.
来看看效果:

3 尾删和头删
Tail deletion and head deletion are also relatively simple for doubly linked lists,Write it together here:
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);
}按部就班的写,Be careful not to miss outok了.
效果:

4 Find and insert and delete)
Find the specific code:
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;
}与单链表一样,This can also change the data:

Check out the specific code:
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;
}这里是在posData is inserted before the bit,实现起来也比较简单.
The specific code to check and delete:
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}It is not difficult to find out after checking and deleting and checking and inserting,Both head and tail plugs can be implemented with check and plug,Both head deletion and tail deletion can be implemented with search and deletion.
头插在 phead->next前插入,尾插在phead前插入,The header delete position is phead->next,The tail deletion position is phead->prev.
This is the method that is commented out when implementing the code above.
5 释放
void ListDestroy(ListNode* phead)
{
ListNode* cur = phead;
ListNode* next = phead->next;
while (cur->next!=phead)
{
free(cur);
cur = next;
next = next->next;
}
}A free linked list is similar to a singly linked list,比较容易理解.
好了,今天的分享就到这里了,如果该文对你有帮助的话能不能3连支持一下博主呢?

边栏推荐
- Deceptive Dice(期望计算)
- AI+医疗:使用神经网络进行医学影像识别分析
- Word箭头上面怎么打字
- random.normal() and random.truncated_normal() in TF
- Word怎么制作一张标准的答题卡?
- 小黑leetcode之旅:94. 二叉树的中序遍历(补充Morris 中序遍历)
- ACM MM 2022 | Cloud2Sketch: Painting with clouds in the sky, AI brush strokes
- TF使用constant生成数据
- Ali Ermi: Without accept, can a TCP connection be established?
- 威纶通触摸屏制作自定义弹出窗口的具体方法(3种)
猜你喜欢

必看设计干货|易知微设计师是怎么做标准可视化设计服务的?
![[Graphic and textual] How to reinstall Win7 system](/img/24/3acccb93e5e219f39477dc77229a58.png)
[Graphic and textual] How to reinstall Win7 system

UE4_定序器控制蓝图对象

Optimization of SQL Statements and Indexes

Skywalking系列学习之Trace Profiling源码分析
![[Generic Programming] Full Detailed Explanation of Templates](/img/9d/7864f999cb2e4edda2ee7723558135.png)
[Generic Programming] Full Detailed Explanation of Templates

小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和

Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week

简单问题窥见数学

浅谈Numpy中的shape、reshape函数的区别
随机推荐
windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
np中的round函数,ceil函数与floor函数
Ehrlich screening method: Counting the number of prime numbers
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
LeetCode26:删除有序数组中的重复项
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
抽象类 or 接口
必看设计干货|易知微设计师是怎么做标准可视化设计服务的?
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides
蔚来杯2022牛客暑期多校训练营7 CFGJ
埃氏筛选法:统计素数个数
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
Word怎么制作双面席卡?使用Word制作双面席卡方法
RHEL7系统修复rm -rf /boot /etc/fstab
编程语言中,取余和取模的区别
MySQL慢查询的多个原因
STC8H development (15): GPIO drive Ci24R1 wireless module
[corctf 2022] section
浅谈Numpy中的shape、reshape函数的区别