当前位置:网站首页>C language_ Double create, pre insert, post insert, traverse, delete
C language_ Double create, pre insert, post insert, traverse, delete
2022-07-06 06:52:00 【Mr_ WangAndy】
C Language to realize the basic operation of double linked list : establish , forward , Post insertion , Traverse , Delete
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *pPre;
struct node *pNext;
}Node;
// Create nodes
Node *create_node(int data)
{
Node *p = (Node *)malloc(sizeof(Node));
if (NULL == p)
{
return NULL;
}
//
p->pPre = NULL;
p->data = data;
p->pNext = NULL;
return p;
}
// Double linked list tail insertion
int insert_tail(Node *pH,Node *new)
{
if(pH == NULL)
{
return -1;
}
Node *p = pH;
int cout = 0;
while(p->pNext)
{
p = p -> pNext;
cout++;
}
p->pNext = new; // next Pointer Association
new->pPre = p; // pre Pointer Association
pH->data = cout + 1;
return 0;
}
// forward
int insert_forward(Node *pH,Node *new)
{
if(pH == NULL)
{
return -1;
}
static int cout = 0;
// Judge whether there is a node behind the head node
if (pH->pNext == NULL)
{
pH->pNext = new;
new->pPre = pH;
cout++;
}
else
{
new->pNext = pH->pNext;
new->pPre = pH;
pH->pNext->pPre = new;
pH->pNext = new;
cout++;
}
pH->data = cout;
return 0;
}
// Traverse
void traversal_dlink(Node *pH)
{
Node *p = pH;
printf(" Traverse backward \n");
while (p->pNext)
{
p = p->pNext;
printf("data=%d.\n",p->data);
}
// Traverse forward again
printf(" Traversal forward \n");
while (p->pPre)
{
printf("data=%d.\n",p->data);
p = p->pPre;
}
}
// Delete node
int delete_node(Node *pH,int data)
{
Node *p = pH;
if (NULL == pH || NULL == pH->pNext)
{
return -1;
}
while (NULL != p->pNext)
{
p = p->pNext;
if (p->data == data)
{
// If the last one is a node
if (p->pNext != NULL)
{
p->pPre->pNext = p->pNext;
p->pNext->pPre = p->pPre;
free(p);
}
else
{
p->pPre->pNext = NULL;
free(p);
}
// If it's not the last node
return 0;
}
}
}
int main()
{
Node *pHeader = create_node(0);
insert_forward(pHeader,create_node(101));
insert_forward(pHeader,create_node(102));
insert_forward(pHeader,create_node(103));
insert_forward(pHeader,create_node(104));
insert_forward(pHeader,create_node(105));
// Traverse
traversal_dlink(pHeader);
delete_node(pHeader,101);
traversal_dlink(pHeader);
return 0;
}

边栏推荐
- CS certificate fingerprint modification
- 机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
- LeetCode - 152 乘积最大子数组
- LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- How to do a good job in financial literature translation?
- Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
- P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
- Fedora/rehl installation semanage
- Day 246/300 SSH connection prompt "remote host identification has changed!"
猜你喜欢

UWA Pipeline 2.2.1 版本更新说明

(practice C language every day) reverse linked list II

CS certificate fingerprint modification

Do you really know the use of idea?

A method to measure the similarity of time series: from Euclidean distance to DTW and its variants

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm

顶测分享:想转行,这些问题一定要考虑清楚!

攻防世界 MISC中reverseMe简述

Introduction and underlying analysis of regular expressions
![[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
随机推荐
[brush questions] how can we correctly meet the interview?
How much is the price for the seal of the certificate
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Biomedical English contract translation, characteristics of Vocabulary Translation
【软件测试进阶第1步】自动化测试基础知识
万丈高楼平地起,每个API皆根基
UDP攻击是什么意思?UDP攻击防范措施
Latex文字加颜色的三种办法
Today's summer solstice
Windows Server 2016 standard installing Oracle
Pallet management in SAP SD delivery process
成功解决TypeError: data type ‘category‘ not understood
Suspended else
How to reconstruct the class explosion caused by m*n strategies?
Simple use of MySQL database: add, delete, modify and query
Lesson 7 tensorflow realizes convolutional neural network
E-book CHM online CS
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype