当前位置:网站首页>C语言_双创建、前插,尾插,遍历,删除
C语言_双创建、前插,尾插,遍历,删除
2022-07-06 06:36:00 【Mr_WangAndy】
C语言实现双链表的基本操作:创建,前插,后插,遍历,删除
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *pPre;
struct node *pNext;
}Node;
// 创建节点
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;
}
// 双链表尾插
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指针关联
new->pPre = p; // pre指针关联
pH->data = cout + 1;
return 0;
}
// 前插
int insert_forward(Node *pH,Node *new)
{
if(pH == NULL)
{
return -1;
}
static int cout = 0;
// 判断头节点后边是否有节点
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;
}
// 遍历
void traversal_dlink(Node *pH)
{
Node *p = pH;
printf("向后遍历\n");
while (p->pNext)
{
p = p->pNext;
printf("data=%d.\n",p->data);
}
// 再向前遍历
printf("向前遍历\n");
while (p->pPre)
{
printf("data=%d.\n",p->data);
p = p->pPre;
}
}
// 删除节点
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 (p->pNext != NULL)
{
p->pPre->pNext = p->pNext;
p->pNext->pPre = p->pPre;
free(p);
}
else
{
p->pPre->pNext = NULL;
free(p);
}
// 如果不是最后一个节点
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));
// 遍历
traversal_dlink(pHeader);
delete_node(pHeader,101);
traversal_dlink(pHeader);
return 0;
}

边栏推荐
- 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
- [Yu Yue education] Dunhuang Literature and art reference materials of Zhejiang Normal University
- Reflex WMS medium level series 3: display shipped replaceable groups
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
- 英语论文翻译成中文字数变化
- 自动化测试环境配置
- Defense (greed), FBI tree (binary tree)
- Basic commands of MySQL
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
- What are the characteristics of trademark translation and how to translate it?
猜你喜欢

商标翻译有什么特点,如何翻译?

ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略

Luogu p2089 roast chicken
![[web security] nodejs prototype chain pollution analysis](/img/b6/8eddc9cbe343f2439da92bf342b0dc.jpg)
[web security] nodejs prototype chain pollution analysis

Making interactive page of "left tree and right table" based on jeecg-boot

CS certificate fingerprint modification
![[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)

Modify the list page on the basis of jeecg boot code generation (combined with customized components)

On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
![[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
随机推荐
Leetcode daily question (1870. minimum speed to arrive on time)
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
LeetCode - 152 乘积最大子数组
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
Difference between backtracking and recursion
机器学习植物叶片识别
Leetcode daily question (971. flip binary tree to match preorder traversal)
SSO process analysis
Simple query cost estimation
ECS accessKey key disclosure and utilization
云服务器 AccessKey 密钥泄露利用
英语论文翻译成中文字数变化
Leetcode - 152 product maximum subarray
A 27-year-old without a diploma, wants to work hard on self-study programming, and has the opportunity to become a programmer?
Summary of leetcode's dynamic programming 4
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
删除外部表源数据
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
端午节快乐Wish Dragon Boat Festival is happy
Today's summer solstice