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

边栏推荐
- It is necessary to understand these characteristics in translating subtitles of film and television dramas
- Data security -- 13 -- data security lifecycle management
- Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
- 我的创作纪念日
- Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
- 机器学习植物叶片识别
- SSO process analysis
- 【软件测试进阶第1步】自动化测试基础知识
- P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
- CS通过(CDN+证书)powershell上线详细版
猜你喜欢

Cobalt Strike特征修改

LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)

University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems

如何做好互联网金融的英语翻译

Advanced MySQL: Basics (1-4 Lectures)

In English translation of papers, how to do a good translation?

Luogu p2089 roast chicken

Biomedical localization translation services

It is necessary to understand these characteristics in translating subtitles of film and television dramas

Making interactive page of "left tree and right table" based on jeecg-boot
随机推荐
SSO process analysis
How effective is the Chinese-English translation of international economic and trade contracts
论文翻译英译中,怎样做翻译效果好?
Leetcode daily question (971. flip binary tree to match preorder traversal)
Leetcode - 152 product maximum subarray
L'Ia dans les nuages rend la recherche géoscientifique plus facile
SQL Server Manager studio (SSMS) installation tutorial
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
MySQL high frequency interview 20 questions, necessary (important)
金融德语翻译,北京专业的翻译公司
Luogu p2089 roast chicken
How to translate biomedical instructions in English
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
How to do a good job in financial literature translation?
LeetCode 739. Daily temperature
SAP SD发货流程中托盘的管理