带头 + 双向 + 循环链表增删查改实现

目录

源码:

List.c文件:

List.h文件:

简单的测试:



很简单,没什么好说的,直接上源码。

源码:

List.c文件:

#include"DLList.h"ListNode* creadNode(LTDataType x)
{ListNode* temp = (ListNode*)malloc(sizeof(ListNode));if (temp == NULL){perror("malloc fail !\n");return -1;}temp->data = x;temp->next = NULL;temp->prev = NULL;return  temp;}// 创建返回链表的头结点.
ListNode* ListCreate()
{ListNode* temp = (ListNode*)malloc(sizeof(ListNode));if (temp == NULL){perror("malloc fail !\n");return -1;}temp->next = temp;temp->prev = temp;temp->data = 0;return  temp;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{//头节点一定不会为空assert(pHead);if (pHead->next == pHead){free(pHead);pHead = NULL;}ListNode* cur = pHead->next;while (cur != pHead){ListNode* next = cur->next;free(cur);cur = next;}printf("销毁成功!");
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{assert(pHead);if (pHead->next == pHead){printf("List is NULL!\n");exit(-1);}printf("哨兵位<=>");ListNode* cur = pHead->next;while (cur != pHead){printf("%d<=>",cur->data);cur = cur->next;}printf("\n");
}// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{ListNode* newNode = creadNode(x);ListNode* tail = pHead->next;while (tail->next != pHead){tail = tail->next;}newNode->next = pHead;pHead->prev = newNode;tail->next = newNode;newNode->prev = tail;
}// 双向链表尾删
void ListPopBack(ListNode* pHead)
{assert(pHead);if (pHead->next == pHead){printf("List is NULL!\n");exit(-1);}ListNode* tail = pHead->prev;ListNode* prev = tail->prev;pHead->prev = prev;prev->next = pHead;free(tail);}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{ListNode* newNode = creadNode(x);newNode->next = pHead->next;pHead->prev = newNode;pHead->next = newNode;newNode->prev = pHead;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{assert(pHead);if (pHead->next == pHead){printf("List is  NULL!\n");exit(-1);}ListNode* firstNode = pHead->next;ListNode* secondNode = firstNode->next;pHead->next = secondNode;secondNode->prev = pHead;free(firstNode);firstNode = NULL;
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{assert(pHead);ListNode* cur = pHead->next;while (cur != pHead){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode *pHead, ListNode* pos, LTDataType x)
{assert(pos);if (pos == pHead){ListPushBack(pHead,x);}ListNode* cur = pHead->next;while (cur != pHead){if (cur->data == pos->data){ListNode* prev = pos->prev;ListNode *newNode  = creadNode(x);prev->next = newNode;newNode->prev = prev;newNode->next = pos;pos->prev = newNode;return;}cur = cur->next;}
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pHead,ListNode* pos)
{assert(pHead);if (pos == pHead->next){ListPopFront(pHead);}if (pos == pHead->prev){ListPopBack(pHead);}ListNode* cur = pHead->next;while (cur != pHead){if (cur->data == pos->data){ListNode* prev = cur->prev;ListNode* next = cur->next;prev->next = next;next->prev = prev;free(cur);return;}cur = cur->next;}
}

List.h文件:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>// 带头 + 双向 + 循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{LTDataType data;struct ListNode* next;struct ListNode* prev;
}ListNode;ListNode* creadNode();
// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pHead,ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pHead,ListNode* pos);

简单的测试:

#include"DLList.h"void test1(ListNode * head) {ListPushBack(head, 1);ListPushBack(head, 2);ListPushBack(head, 3);ListPushBack(head, 4);ListPushBack(head, 5);ListPopBack(head);ListPrint(head);ListPopBack(head);ListPrint(head);ListPopBack(head);ListPrint(head);ListPopBack(head);ListPrint(head);ListPopBack(head);ListPrint(head);ListPopBack(head);ListPrint(head);}void test2(ListNode* head) {ListPushBack(head, 1);ListPushBack(head, 2);ListPushBack(head, 3);ListPushBack(head, 4);ListPushBack(head, 5);ListPopFront(head);ListPrint(head);
}void test3(ListNode* head) {ListPushBack(head, 1);ListPushBack(head, 2);ListPushBack(head, 3);ListPushBack(head, 4);ListPushBack(head, 5);ListFind(head,2);ListFind(head,7);ListPrint(head);
}void test4(ListNode* head) {ListPushBack(head, 1);ListPushBack(head, 2);ListPushBack(head, 3);ListPushBack(head, 4);ListPushBack(head, 5);ListInsert(head, ListFind(head, 2),10);ListInsert(head, ListFind(head, 1),10);ListInsert(head, ListFind(head, 5),10);ListInsert(head, ListFind(head, 6),10);ListPrint(head);
}void test5(ListNode* head) {ListPushBack(head, 1);ListPushBack(head, 2);ListPushBack(head, 3);ListPushBack(head, 4);ListPushBack(head, 5);ListErase(head,ListFind(head,5));ListErase(head,ListFind(head,1));ListErase(head,ListFind(head,2));ListPrint(head);
}int main()
{ListNode* head = ListCreate();//test1(head);//test2(head);//test3(head);//test4(head);test5(head);ListDestory(head);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/641733.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

力扣!30天60道(第2天)

第1题(1.22) &#xff1a;两数之和 解法一&#xff1a;暴力破解 #include <iostream> #include <vector> #include <map> using namespace std;class Solution { public:vector<int> twoSum1(vector<int>& nums, int target) {for (int i …

Java8 安装

> 新版任你发&#xff0c;我用JAVA8 1. 下载jdk包 2. 解压到自己要的位置 3. 配置环境变量 export JAVA_HOME/usr/lib/jvm/jdk1.8.0_202/ export JRE_HOME${JAVA_HOME}/jre export CLASSPATH.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH${JAVA_HOME}/bin:$PATH

GraphicsMagick 的 OpenCL 开发记录(十八)

文章目录 gm benchmark性能比较 <2022-04-07 Thu> gm benchmark性能比较 仅运行一次缩放图片的话gm-ocl&#xff08;启用了硬件加速&#xff09;的速度远小于gm&#xff08;没有硬件加速&#xff0c;下同&#xff09;&#xff0c;而迭代100次的话&#xff0c;gm-ocl速度…

Java项目:基于ssm框架实现的电影评论系统(ssm+B/S架构+源码+数据库+毕业论文)

一、项目简介 本项目是一套ssm826基于ssm框架实现的电影评论系统&#xff0c;主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#x…

grpcui安装使用

官网地址&#xff1a;https://github.com/fullstorydev/grpcui 安装命令&#xff1a; go get github.com/fullstorydev/grpcui go install github.com/fullstorydev/grpcui/cmd/grpcui ./bin/grpcui --import-path/home/xx/proto -proto xx.proto --plaintext 10.2.9.112:1…

Leetcode 17.电话号码的字母组合 - Letter Combinations of a Phone Number - Python - 回溯法

解题思路&#xff1a; 回溯法三部曲&#xff1a; 1.回溯函数的参数&#xff1b; 2.确定终止条&#xff1b; 3.确定单层遍历逻辑&#xff1b; 注意&#xff1a; 这道题有一点很有趣的地方是&#xff0c;你需要先遍历给定的digits字符串&#xff0c;此处可想象成树型结构。…

GEE脚本——GEE中如何查询历史脚本和防丢失记录

很多时候我们会发现我们之前编辑的脚本不见了,本来已经编辑好了但是发现原来的脚本更完美,至于诸如此类的一些问题,当我们在使用GEE中的代码编译器的时候会时不时的出现,这里我们也无需过多担心,这里只要你首次将代码保存在你所创建的项目当中的时候我们就可以查看以往每一…

【爬虫、数据可视化实战】以“人口”话题为例爬取实时微博数据并进行舆情分析

前言&#xff1a; 近期在weibo上讨论的比较热的话题无非就是“人口”了。TaoTao也看了一些大家发的内容。但是感觉单纯的看文字内容不能很直观的反应出来大家的关切。索性就使用爬虫对数据进行爬取&#xff0c;同时结合着数据可视化的方式让数据自己开口说话。那么接下来就让我…

Python源码49:海龟画图turtle画美国旗

---------------turtle源码集合--------------- Python教程91&#xff1a;关于海龟画图&#xff0c;Turtle模块需要学习的知识点 Python源码45&#xff1a;海龟画图turtle画雪容融 Python源码44&#xff1a;海龟画图turtle&#xff0c;画2022卡塔尔世界杯吉祥物 Python教程…

《WebKit 技术内幕》学习之十(3): 插件与JavaScript扩展

3 JavaScript引擎的扩展机制 3.1 混合编程 混合编程由来已久&#xff0c;因为浏览器能力的不足&#xff0c;特别是以前的浏览器甚至不支持内嵌视频和音频等技术&#xff0c;所以导致需要Flash等插件来扩展网页的能力。当然Flash插件是由第三方提供的&#xff0c;大家都可以使…

【FINEBI】finebi中常用图表类型及其适用场景

柱状图&#xff08;Bar Chart&#xff09;&#xff1a; 比较不同类别或组之间的数量差异&#xff1a;柱状图可以用于比较不同产品、地区、时间段等的销售额、市场份额等。 显示不同时间段的数据变化&#xff1a;通过绘制柱状图&#xff0c;可以观察到销售额、网站流量等随时间…

前端JavaScript篇之实现有序数组原地去重方法有哪些?

目录 实现有序数组原地去重方法有哪些&#xff1f;方法一&#xff1a;使用 Set 数据结构代码实现&#xff1a;思路说明&#xff1a; 方法二&#xff1a;使用双指针遍历代码实现&#xff1a;思路说明&#xff1a; 实现有序数组原地去重方法有哪些&#xff1f; 在 JavaScript 中…

机器学习神器:Sklearn详解

引言 Sklearn (全称 Scikit-Learn) 是基于 Python 语言的机器学习工具。它建立在 NumPy, SciPy, Pandas 和 Matplotlib 之上&#xff0c;里面的 API 的设计非常好&#xff0c;所有对象的接口简单&#xff0c;很适合新手上路。 在 Sklearn 里面有六大任务模块&#xff1a;分别是…

怎样的安全数据交换系统 可以支持信创环境?

首先&#xff0c;我来看看&#xff0c;什么是安全数据交换系统&#xff1f;安全数据交换系统是一种专门设计用于在不同网络环境之间安全传输数据的技术解决方案。它确保数据在传输过程中的完整性、机密性和可用性&#xff0c;同时遵守相关的数据保护法规和行业标准。 那么&…

透明拼接屏显示:技术与应用

在当今的数字化时代&#xff0c;显示技术已成为我们日常生活和工作中的重要组成部分。透明拼接屏作为一种新型的显示技术&#xff0c;以其独特的透明设计和灵活的拼接特性&#xff0c;正逐渐在各个领域得到广泛应用&#xff0c;尼伽小编&#xff0c;将深入探讨透明拼接屏显示的…

灵感无限!12个设计师最爱的网站推荐,覆盖UX、网页设计和国外设计精华

即时设计资源广场 即时设计资源广场是中国优秀的UI设计网站&#xff0c;全中文环境&#xff0c;非常适合中国人使用。UI设计网站即时设计资源广场内置阿里、字节、腾讯、京东、谷歌、华为等设计系统&#xff0c;3000多个UI组件库&#xff0c;每月更新数百个高质量模板&#xf…

websocket服务端本地部署

文章目录 1. Java 服务端demo环境2. 在pom文件引入第三包封装的netty框架maven坐标3. 创建服务端,以接口模式调用,方便外部调用4. 启动服务,出现以下信息表示启动成功,暴露端口默认99995. 创建隧道映射内网端口6. 查看状态->在线隧道,复制所创建隧道的公网地址加端口号7. 以…

空气净化器or宠物空气净化器?五款猫用空气净化器优质推荐!

作为一个养猫家庭的主人&#xff0c;每天都要面对清理猫砂盘的挑战&#xff0c;这种令人难以形容的气味实在让人难以忍受。尤其是家里有小孩和老人&#xff0c;他们可能会出现过敏性鼻炎等问题&#xff0c;而抵抗力较差的人更容易受到影响。此外&#xff0c;换毛季节到来时&…

性能优化(CPU优化技术)-NEON指令介绍

「发表于知乎专栏《移动端算法优化》」 本文主要介绍了 NEON 指令相关的知识&#xff0c;首先通过讲解 arm 指令集的分类&#xff0c;NEON寄存器的类型&#xff0c;树立基本概念。然后进一步梳理了 NEON 汇编以及 intrinsics 指令的格式。最后结合指令的分类&#xff0c;使用例…

数据运营项目1

下面是一些注意事项&#xff1a; 10w以上就不要用excel去做了会很卡很慢&#xff0c;可以考虑powerbi&#xff0c;用powerbi解决RFM模型 Powerbi替换时&#xff0c;替换没不写就行了&#xff0c;不是空值 主页分组依据就是拉数据透视表 所有工具都要打上双引号 文本不能做减…