当前位置:网站首页>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
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- Suspended else
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
- Data security -- 13 -- data security lifecycle management
- Basic commands of MySQL
- LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
- After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
- hydra常用命令
- In English translation of papers, how to do a good translation?
猜你喜欢
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
云上有AI,让地球科学研究更省力
L'Ia dans les nuages rend la recherche géoscientifique plus facile
How effective is the Chinese-English translation of international economic and trade contracts
Reflex WMS medium level series 3: display shipped replaceable groups
Introduction and underlying analysis of regular expressions
SQL Server Manager studio (SSMS) installation tutorial
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
[advanced software testing step 1] basic knowledge of automated testing
随机推荐
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
UWA Pipeline 2.2.1 版本更新说明
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
云上有AI,让地球科学研究更省力
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
我的创作纪念日
Py06 dictionary mapping dictionary nested key does not exist test key sorting
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
基于购买行为数据对超市顾客进行市场细分(RFM模型)
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
kubernetes集群搭建Zabbix监控平台
Day 248/300 thoughts on how graduates find jobs
What is the difference between int (1) and int (10)? Senior developers can't tell!
Machine learning plant leaf recognition
SQL Server Manager studio (SSMS) installation tutorial
hydra常用命令
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中