当前位置:网站首页>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;
}
边栏推荐
- 今日夏至 Today‘s summer solstice
- Tms320c665x + Xilinx artix7 DSP + FPGA high speed core board
- 机器学习植物叶片识别
- 女生学软件测试难不难 入门门槛低,学起来还是比较简单的
- How effective is the Chinese-English translation of international economic and trade contracts
- Summary of leetcode's dynamic programming 4
- 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
- Py06 dictionary mapping dictionary nested key does not exist test key sorting
- 国际经贸合同翻译 中译英怎样效果好
猜你喜欢
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
My creation anniversary
Making interactive page of "left tree and right table" based on jeecg-boot
Tms320c665x + Xilinx artix7 DSP + FPGA high speed core board
如何做好互联网金融的英语翻译
Apple has open source, but what about it?
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
Office doc add in - Online CS
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
如何做好金融文献翻译?
随机推荐
Machine learning plant leaf recognition
LeetCode 731. My schedule II
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
基于购买行为数据对超市顾客进行市场细分(RFM模型)
Day 248/300 关于毕业生如何找工作的思考
Summary of leetcode's dynamic programming 4
MySQL5.72.msi安装失败
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
Address bar parameter transmission of list page based on jeecg-boot
Tms320c665x + Xilinx artix7 DSP + FPGA high speed core board
How effective is the Chinese-English translation of international economic and trade contracts
Fedora/rehl installation semanage
Leetcode daily question (1870. minimum speed to arrive on time)
SSO process analysis
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
Financial German translation, a professional translation company in Beijing
[brush questions] how can we correctly meet the interview?
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems