当前位置:网站首页>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;
}

边栏推荐
- How much is the price for the seal of the certificate
- 删除外部表源数据
- SSO process analysis
- How to do a good job in financial literature translation?
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
- 简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
- Fedora/rehl installation semanage
- Day 246/300 SSH connection prompt "remote host identification has changed!"
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- My seven years with NLP
猜你喜欢

Phishing & filename inversion & Office remote template

How much is the price for the seal of the certificate

机器学习植物叶片识别
![[daily question] 729 My schedule I](/img/6b/a9fef338ac09caafe628023f066e1f.png)
[daily question] 729 My schedule I

Reflex WMS medium level series 3: display shipped replaceable groups

19.段页结合的实际内存管理

Leetcode daily question (971. flip binary tree to match preorder traversal)

hydra常用命令

CS certificate fingerprint modification

雲上有AI,讓地球科學研究更省力
随机推荐
Latex文字加颜色的三种办法
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
UDP攻击是什么意思?UDP攻击防范措施
Leetcode daily question (1997. first day where you have been in all the rooms)
[advanced software testing step 1] basic knowledge of automated testing
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
PCL实现选框裁剪点云
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
C语言_双创建、前插,尾插,遍历,删除
hydra常用命令
电子书-CHM-上线CS
Chapter 7 - thread pool of shared model
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
PCL realizes frame selection and clipping point cloud
UWA Pipeline 2.2.1 版本更新说明
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
【每日一题】729. 我的日程安排表 I
Day 248/300 关于毕业生如何找工作的思考