当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
SQL Server Manager studio (SSMS) installation tutorial
Office doc add in - Online CS
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
How much is the price for the seal of the certificate
What are the characteristics of trademark translation and how to translate it?
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
19.段页结合的实际内存管理
《从0到1:CTFer成长之路》书籍配套题目(周更)
Biomedical English contract translation, characteristics of Vocabulary Translation
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
随机推荐
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
GET 和 POST 请求类型的区别
Every API has its foundation when a building rises from the ground
My creation anniversary
AI on the cloud makes earth science research easier
[advanced software testing step 1] basic knowledge of automated testing
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
BUU的MISC(不定时更新)
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
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
医疗软件检测机构怎么找,一航软件测评是专家
Pallet management in SAP SD delivery process
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
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
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
CS passed (cdn+ certificate) PowerShell online detailed version
CS certificate fingerprint modification
How to reconstruct the class explosion caused by m*n strategies?
How much is the price for the seal of the certificate