当前位置:网站首页>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;
}
边栏推荐
- 如何做好互联网金融的英语翻译
- SAP SD发货流程中托盘的管理
- 英语论文翻译成中文字数变化
- [ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
- Data security -- 13 -- data security lifecycle management
- Advanced MySQL: Basics (1-4 Lectures)
- Fledgling Xiao Li's 103rd blog CC2530 resource introduction
- Engineering organisms containing artificial metalloenzymes perform unnatural biosynthesis
- Monotonic stack
- Today's summer solstice
猜你喜欢
【软件测试进阶第1步】自动化测试基础知识
SQL Server Manager studio (SSMS) installation tutorial
Chapter 7 - thread pool of shared model
CS通过(CDN+证书)powershell上线详细版
Transfert des paramètres de la barre d'adresse de la page de liste basée sur jeecg - boot
今日夏至 Today‘s summer solstice
翻译公司证件盖章的价格是多少
My creation anniversary
生物医学英文合同翻译,关于词汇翻译的特点
Summary of leetcode's dynamic programming 4
随机推荐
雲上有AI,讓地球科學研究更省力
国际经贸合同翻译 中译英怎样效果好
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
一文读懂简单查询代价估算
Summary of leetcode's dynamic programming 4
SSO process analysis
生物医学英文合同翻译,关于词汇翻译的特点
字幕翻译中翻英一分钟多少钱?
Basic commands of MySQL
云服务器 AccessKey 密钥泄露利用
ECS accessKey key disclosure and utilization
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
翻译公司证件盖章的价格是多少
Fedora/REHL 安装 semanage
论文摘要翻译,多语言纯人工翻译
CS通过(CDN+证书)powershell上线详细版
如何做好金融文献翻译?
CS-证书指纹修改
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
SQL Server Manager studio (SSMS) installation tutorial