当前位置:网站首页>Double linked list: initialize insert delete traversal
Double linked list: initialize insert delete traversal
2022-07-01 00:39:00 【qq_ fifty million one hundred and four thousand nine hundred an】
Double linked list cannot be accessed randomly , Search by bit , Search by value can only be realized by traversal . Time complexity 0(n)
Initialization of double linked list
// Initialization of double linked list ( Leading node )
typedef struct DNode{ // Define double linked list node type
ElemType data; // Each node stores a data element 【data Called a data field 】
struct DNode *prior,*next;// Precursor and successor pointers
}LNode,*LinkList;
// Initialize double linked list
bool InitDLinkList(DLinkList &L){
L = (DNode *)malloc(sizeof(DNode));// Assign a head node
if (L==NULL)// Out of memory , Allocation failed
return false;
L->prior = NULL;// The first node prior Never point to NULL
L->next = NULL;// There is no node after the head node
return true;
}
void testDLinkLink(){
// Initialize double linked list
DLinkLink L;
InitDLinkList(L);
// Subsequent code ...
}
// Judge whether the double linked list is empty
bool Empty(DLinkList L){
if (L->next == NULL)
return true;
else
return false;
}
Insertion of double linked list
【 Insertion of double linked list 】
// stay p Insert after the node s node
bool InsertNextDNode(DNode *p,DNode *s){
if (p==NULL || s==NULL)// Illegal parameters
return false;
s->next = p->next;
if (p->next != NULL)// If p A node has a successor node
p->next->prior = s;
s->prior = p;
p->next = s;
return = s;
}
【 Deletion of double linked list 】
【 Deletion of double linked list 】
// Delete p The successor node of a node
bool DeleteNextDNode(DNode *p){
if (p==NULL)
return false;
DNode *q = p->next;// find p The successor node of q
if (q==NULL)
return false;//p No successor nodes
p->next = q->next;
if (q->next != NULL)//q Node is not the last node
q->next->prior = p;
free(q);// Release space
return true;
}
void DestoryList(DLinklist &L){
// Circularly release each data node
while (L->next != NULL)
DeleteNextDNode(L);
free(L);// Release the head node The header node can be released only when the table is destroyed
L =NULL;// The head pointer points to NULL
}
【 Traversal of double linked list 】
【 Traversal of double linked list 】
// Backward traversal
while(p!=NULL){
// To a node p Do the corresponding treatment Such as : Print
p = p->next;
}
// Forward traversal
while(p!=NULL){
// To a node p Do the corresponding treatment
p = p->prior;
}
// Forward traversal -- Skip over node
while(p->prior!=NULL){
// To a node p Do the corresponding treatment
p = p->prior;
}
边栏推荐
- The programmer's girlfriend gave me a fatigue driving test
- 2022-2028 global capsule shell industry research and trend analysis report
- What should I do without 50W bride price
- 双链表:初始化 插入 删除 遍历
- C#生成putty格式的ppk文件(支持passphrase)
- 获取屏幕高度
- 给按钮的边框和文字设置不同的背景色
- Ranger plug-in development (Part 2)
- [DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
- P4学习——Basic Tunneling
猜你喜欢
2022-2028 global electric yacht industry research and trend analysis report
Gateway service gateway
Deployment of mini version message queue based on redis6.0
魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
Mindjet mindmanager2022 mind map decompression installer tutorial
[designmode] factory pattern
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
HDU 2488 A Knight's Journey(DFS)
Pytorch auto derivation
New trend of embedded software development: Devops
随机推荐
Random ball size, random motion collision
剑指 Offer 19. 正则表达式匹配
CentOS install MySQL
Member management applet actual development 07 page Jump
The principle and related problems of acid in MySQL
问题解决:如何管理线程私有(thread_local)的指针变量
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Combining online and offline, VR panorama is a good way to transform furniture online!
C WinForm program interface optimization example
ABAQUS 2022 latest edition - perfect realistic simulation solution
MySQL storage engine
写给 5000 粉丝的一封信!
Oracle-表的创建与管理
合适的工作就是好工作
Redis - how to understand publishing and subscribing
2022-2028 global plant peptone industry research and trend analysis report
Gateway service gateway
第53章 从业务逻辑实现角度整体性理解程序
Mindjet mindmanager2022 mind map decompression installer tutorial
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise