当前位置:网站首页>[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连支持一下博主呢?

边栏推荐
- SecureCRT强制卸载
- 埃氏筛选法:统计素数个数
- Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
- 蔚来杯2022牛客暑期多校训练营7 CFGJ
- Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
- knn到底咋回事?
- cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
- 别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
- Referenced file contains errors 完美解决方法
- Leetcode 93 复原IP地址
猜你喜欢

Ali Ermi: Without accept, can a TCP connection be established?
6个规则去净化你的代码

PMP daily practice | didn't lost a 8.9 (including agile + multi-select)

Word箭头上面怎么打字

别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿

如何让您的公司内容满足 GDPR 合规性

Several ways to draw timeline diagrams

LoRa Basics无线通信技术和应用案例详解

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

Optimization of SQL Statements and Indexes
随机推荐
TF generates uniformly distributed tensor
如何让您的公司内容满足 GDPR 合规性
编程语言中,取余和取模的区别
random.normal() and random.truncated_normal() in TF
Application of Acrel5000web Energy Consumption System in a College-Susie Week
TF中使用zeros(),ones(), fill()方法生成数据
Word怎么制作双面席卡?使用Word制作双面席卡方法
威纶通触摸屏制作自定义弹出窗口的具体方法(3种)
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
Ali Ermi: Without accept, can a TCP connection be established?
Unity2D_背景粒子效果
哪款C语言编译器(IDE)适合初学者?
STC8H development (15): GPIO drive Ci24R1 wireless module
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
How to fix Windows 11 not finding files
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
蔚来杯2022牛客暑期多校训练营7 CFGJ
人人都可以DIY的大玩具,宏光MINIEV GAMEBOY产品力强,出行新装备
场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon