当前位置:网站首页>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;
}
边栏推荐
- Windows Server 2016 standard installing Oracle
- Reflex WMS中阶系列3:显示已发货可换组
- What are the commonly used English words and sentences about COVID-19?
- [English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
- Day 246/300 SSH connection prompt "remote host identification has changed!"
- hydra常用命令
- Call, apply, bind rewrite, easy to understand with comments
- [English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
猜你喜欢
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
SQL Server Manager studio (SSMS) installation tutorial
万丈高楼平地起,每个API皆根基
Database basics exercise part 2
机器学习植物叶片识别
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
How to convert flv file to MP4 file? A simple solution
雲上有AI,讓地球科學研究更省力
前缀和数组系列
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
随机推荐
Latex文字加颜色的三种办法
Use shortcut LNK online CS
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
接口自动化测试框架:Pytest+Allure+Excel
SSO process analysis
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
攻防世界 MISC中reverseMe简述
How to reconstruct the class explosion caused by m*n strategies?
RichView TRVStyle 模板样式的设置与使用
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
云上有AI,让地球科学研究更省力
Automated test environment configuration
编译,连接 -- 笔记 -2
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
[Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Office doc add in - Online CS