双向链表的创建、插入和删除、宏定义函数

server/2024/9/24 15:17:14/

摘要:本文介绍双向链表的实现:创建、插入、删除,以及宏定义版本

所谓双链表即每个节点有两个指针:prev指向前一个节点,next指向后一个节点

struct node {int data;struct node *prev;struct node *next;
};

首先需要创建节点:

struct node* create_node(int data) {struct node* newnode = (struct node*)malloc(sizeof(struct node));if(newnode == NULL) {printf("内存分配失败")exit(1);}newnode->prev = NULL;newnode->next = NULL;newnode->data = data;return newnode;
}

对节点进行插入操作:

1.头插法

void insert(struct node **head, int data) {struct node *newnode = create_node(data);if (*head == NULL){*head = newnode;return;}newnode->next = *head;(*head)->prev = newnode;*head = newnode;
}

2.尾插法:

void insert(struct node **head, int data) {struct node *newnode = create_node(data);struct node *current = *head;if (current->next != NULL) {current = current->next;}current->next = newnode;newnode->prev = current;
}

删除一个节点:分为表头、表中、表尾

void delete(struct node**head, int data) {int status = 0;struct node *current = *head;while (current != NULL) {//从头节点遍历到最后一个节点if (current->data == data) {status = 1;//找到了break;}current = current->next;}if (!status) printf("未找到该数据所在节点\n");else {//节点在表尾if(current->next == NULL) {if (current->prev != NULL) // 先判断current->prev->next = NULL;free(current);}else if(current->prev == NULL) {//*******节点在表头!需要修改*head!***********head = current->next;if(current->next != NULL)current->next->prev = NULL;//考虑删除该节点为唯一的节点的情况free(current);}else{//节点在表中current->prev->next = current->next;current->next->prev = current->prev;free(current);}(current) = NULL; /* 将current置为NULL 避免悬空指针(悬空指针指向一个已经被释放的空间的地址,没有访问权限)*/}
}

宏定义头插法: list是链表头指针,item是要插入的指针

#define LIST_INSERT(item, list) do {\if ((item) != NULL) {\(item)->prev = NULL;\if ((list) != NULL) {\(item)->next = (list);\(list)->prev = (item);\} else {\(item)->next = NULL;\}\(list) = (item);\ // 更新头指针}\
} while(0)

宏定义尾插法:

#define LIST_APPEND(item, list) do {\if ((item) != NULL) {\if ((list) == NULL) {\(list) = (item);\(item)->prev = NULL;\} else {\ListNode *temp = (list);\while (temp->next != NULL) {\temp = temp->next;\}\temp->next = (item);\(item)->prev = temp;\}\(item)->next = NULL;\ }\
} while(0)

宏定义删除节点: 提供要删除的指针,进行删除


#define LIST_REMOVE(item, list) do {\if ((item) != NULL) {\if ((item)->prev != NULL) (item)->prev->next = (item)->next;\if ((item)->next != NULL) (item)->next->prev = (item)->prev;\if ((list) == (item)) (list) = (item)->next;\(item)->prev = (item)->next = NULL;\}\
} while(0)

宏定义查找删除节点:根据节点的data数据,先查找后删除:

#define DELETE_NODE(head, data) do {\int status = 0;\struct node *current = *(head);\while (current != NULL) {\if (current->data == data) {\status = 1;\break;\}\current = current->next;\}\if (!status) {\printf("未找到该数据所在节点\n");\} else {\struct node *temp = current;\if (current->next == NULL) {\if (current->prev != NULL)\current->prev->next = NULL;\free(temp);\} else if (current->prev == NULL) {\*(head) = current->next;\current->next->prev = NULL;\free(temp);\} else {\current->prev->next = current->next;\current->next->prev = current->prev;\free(temp);\}\(current) = NULL; /* 将current置为NULL 避免悬空指针*/\}\
} while(0)

推荐学习https://xxetb.xetslk.com/s/p5Ibb


http://www.ppmy.cn/server/32271.html

相关文章

安卓Flutter框架:一种高效跨平台移动应用解决方案的探究

摘要 在移动开发领域,跨平台技术正变得越来越重要。谷歌推出的Flutter框架以其高效的开发模式和高性能的运行时表现,为开发者提供了一个极具吸引力的解决方案。本文将概述Flutter框架的核心概念,分析其优缺点,并探讨其在各种应用场…

代码随想录算法训练营DAY46|C++动态规划Part8|139.单词拆分、多重背包理论基础、背包问题总结篇

文章目录 139.单词拆分思路CPP代码 多重背包理论基础处理输入把所有个数大于1的物品展开成1个开始迭代,计算dp数组代码优化 背包问题总结篇 139.单词拆分 力扣题目链接 文章讲解:139.单词拆分 视频讲解:你的背包如何装满?| LeetCo…

【NR RedCap】Release 18标准中对5G RedCap的增强

博主未授权任何人或组织机构转载博主任何原创文章,感谢各位对原创的支持! 博主链接 本人就职于国际知名终端厂商,负责modem芯片研发。 在5G早期负责终端数据业务层、核心网相关的开发工作,目前牵头6G技术研究。 博客内容主要围绕…

JAVA前端快速入门基础_javascript入门(02)

写在前面:本文用于快速学会简易的JS,仅做扫盲和参考作用 1.JavaScript函数 什么是函数:执行特定任务的代码块 1.1定义: 使用function来进行定义(类似于python里面的def 或者java和c里面的void,int这些返回类型开头)。定义规则如下: func…

如何构建用于从收据中提取信息的生成式人工智能工具

原文地址:how-to-build-a-generative-ai-tool-for-information-extraction-from-receipts 使用 LangChain 和 OpenAI 工具从 Google Drive 中存储的收据图像中提取结构化信息 2024 年 4 月 10 日 纸质收据有各种样式和格式,是自动信息提取的一个有趣目…

VBA 拆分Excel中的各sheet为文件

一. 方式1 xlOpenXMLWorkbook:.xlsx格式的文件xlWorkbookDefault:当前Excel的格式(当前Excel是什么格式,被拆分出的sheet页所生成的文件就是什么格式)"\":可以使用Application.PathSeparator代替 Sub 拆分工作表() 初…

wordpress子比主题美化-为图文列表封面添加动态缩略图特效 多种效果演示

wordpress子比主题-为图文列表文章封面添加动态缩略图特效 给自己子比主题加一个列表文章封面添加动态缩略图 直接复制以下代码,添加到主题自定义CSS代码中即可,下图为效果演示 wordpress子比主题-为图文列表文章封面添加动态缩略图特效 给自己子比主题…

富格林:细节决定能否安全出金

富格林悉知,投资者都希望在现货黄金交易市场中获利,但并非所有投资者都能实现获利的心愿,有时候忽略一些细节问题也会影响最终的投资效果。投资者应该注重细节实现安全出金才是我们进行投资的最终目标。下面富格林将总结一些注重细节实现安全…