当前位置: 首页 > news >正文

双向链表代码(带哨兵位循环/不带哨兵位不循环

 以下代码全为本人所写,如有错误,很正常,还请指出,

目录

带哨兵位循环

 test.c

DLList.c 

 DLList.h

 不带哨兵位不循环

test.c 

 DLList.c

 DLList.h


带哨兵位循环

 test.c

#define  _CRT_SECURE_NO_WARNINGS#include"DLlist.h"int main()
{//初始化链表DL* DLList = DLListInit();//因为带哨兵位,不需要修改头节点,修改的是头节点指向的结构体的内容//传值就行,传的是DLList这个指针存的地址SLPushBack(DLList,8);SLPushFront(DLList, 5);SLPushFront(DLList, 6);SLPushBack(DLList,7);SLprint(DLList);//6->5->8->7SLPopFrpnt(DLList);SLPopBack(DLList);SLPushBack(DLList, 7);SLprint(DLList);//5->8->7DL* pos=SLFind(DLList,8);SLInsertBack(DLList,pos,9);//后插    5->8->9->7SLprint(DLList);pos = SLFind(DLList, 8);SLInsertFront(DLList, pos, 10);  //前插  5->10->8->9->7SLprint(DLList);SLErase(DLList,pos);//删除  5->10->9->7SLprint(DLList);DListDestory(DLList);return 0;
}

DLList.c 

#define  _CRT_SECURE_NO_WARNINGS#include"DLlist.h"DL* BuyDLNode()
{DL* newcode = (DL*)malloc(sizeof(DL));return newcode;//返回的是这个指针存的地址
}DL* DLListInit()
{DL* newcode = BuyDLNode();//存的是malloc开辟空间的地址newcode->val = -1;newcode->next = newcode;newcode->prev = newcode;return newcode;
}void SLPushFront(DL* pphead, SLdatatype x)
{assert(pphead);DL* newcode = BuyDLNode();newcode->prev = pphead;newcode->next = pphead->next;newcode->val = x;DL* first = pphead->next;pphead->next = newcode;first->prev = newcode;
}void SLPushBack(DL* pphead, SLdatatype x)
{assert(pphead);DL* newcode = BuyDLNode();newcode->next = pphead;newcode->prev = pphead->prev;newcode->val = x;DL* end = pphead->prev;pphead->prev = newcode;end->next = newcode;
}void SLPopFrpnt(DL* pphead)
{assert(pphead);DL* first = pphead->next;DL* second = pphead->next->next;free(first);pphead->next = second;second->prev = pphead;
}void SLPopBack(DL* pphead)
{assert(pphead);DL* end = pphead->prev;DL* end_second = pphead->prev->prev;free(end);pphead->prev = end_second;end_second->next = pphead;
}DL* SLFind(DL* pphead, SLdatatype x)
{assert(pphead);DL* tail = pphead->next;while (tail->val != x){tail = tail->next;if (tail == pphead){printf("该链表内不存在该数据\n");return NULL;}}return tail;
}void SLInsertBack(DL* pphead, DL* pos, SLdatatype x)
{assert(pphead);assert(pos);DL* tail = pphead->next;while (tail != pos)//tail找到对应的节点   后插{tail = tail->next;}DL* newcode = BuyDLNode();newcode->val = x;newcode->next = tail->next;newcode->prev = tail;DL* Back = tail->next;Back->prev = newcode;tail->next = newcode;
}void SLInsertFront(DL* pphead, DL* pos, SLdatatype x)
{assert(pphead);assert(pos);DL* tail = pphead->next;while (tail != pos)//tail找到对应的节点   前插{tail = tail->next;}DL* newcode = BuyDLNode();newcode->val = x;newcode->next = tail;newcode->prev = tail->prev;DL* Front = tail->prev;Front->next = newcode;tail->prev = newcode;
}void SLErase(DL* pphead, DL* pos)
{assert(pphead);assert(pos);DL* tail = pphead->next;while (tail != pos)//tail找到对应的节点  要删除的节点{tail = tail->next;}DL* Front = tail->prev;DL* Back = tail->next;free(tail);Front->next = Back;Back->prev = Front;
}void SLprint(DL* pphead)
{DL* tail = pphead->next;printf("Sentinel->");while (tail != pphead){printf("%d->", tail->val);tail = tail->next;}printf("NULL\n");
}void DListDestory(DL* pphead)
{assert(pphead);DL* tail = pphead->next;while (tail != pphead){//先保存DL* tmp = tail->next;free(tail);tail = tmp;}free(pphead);
}

 DLList.h

#define  _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>typedef int SLdatatype;typedef struct ListNode
{SLdatatype val;struct ListNode* next;struct ListNode* prev;
}DL;DL* BuyDLNode();DL* DLListInit();void SLPushFront(DL* pphead, SLdatatype x);void SLPushBack(DL* pphead, SLdatatype x);void SLPopFrpnt(DL* pphead);void SLPopBack(DL* pphead);DL* SLFind(DL* pphead, SLdatatype x);void SLInsertBack(DL* pphead, DL* pos, SLdatatype x);void SLInsertFront(DL* pphead, DL* pos, SLdatatype x);void SLErase(DL* pphead, DL* pos);void SLprint(DL* pphead);void DListDestory(DL* pphead);

 不带哨兵位不循环

test.c 

#define  _CRT_SECURE_NO_WARNINGS#include"DLList.h"int main()
{DL* DLList = NULL;DLPushFront(&DLList, 6);DLPushFront(&DLList, 5);DLPushBack(&DLList, 7);DLPushBack(&DLList, 8);DLprint(DLList);DLPopFront(&DLList);DLprint(DLList);DLPopBack(&DLList);DLprint(DLList);DLPushBack(&DLList, 10);DLPushBack(&DLList, 11);DLprint(DLList);DL* pos= DListFind(DLList, 10);DLEarse(&DLList, pos);DLprint(DLList);pos = DListFind(DLList, 7);DLInsertBack(&DLList, pos, 12);//任意位置后插DLprint(DLList);DLInsertFront(&DLList, pos, 13);//任意位置前插DLprint(DLList);pos = DListFind(DLList, 6);Modify(&DLList,pos,20);DLprint(DLList);//释放链表DListDestory(&DLList);return 0;
}

 DLList.c

#define  _CRT_SECURE_NO_WARNINGS#include"DLList.h"void DLPushFront(DL** pphead, SLdatatype x)
{assert(pphead);DL* newcode = (DL*)malloc(sizeof(DL));DL* tail = *pphead;if (newcode == NULL){perror("malloc error\n");return;}//if (*pphead == NULL)//{//赋值newcode->val = x;newcode->prev = NULL;newcode->next = *pphead;//更换头节点*pphead = newcode;return;//	}return;
}void DLPushBack(DL** pphead, SLdatatype x)
{assert(pphead);DL* newcode = (DL*)malloc(sizeof(DL));if (newcode == NULL){perror("malloc error\n");return;}if (*pphead == NULL){*pphead = newcode;(*pphead)->val = x;(*pphead)->next = NULL;(*pphead)->next = NULL;return;}DL* tail = *pphead;while (tail->next)//找到最后一个节点{tail = tail->next;}tail->next = newcode;newcode->val = x;newcode->prev = tail;newcode->next = NULL;return;
}void DLPopFront(DL** pphead)
{assert(pphead);assert(*pphead);DL* tail = *pphead;*pphead = (*pphead)->next;//释放第一个节点free(tail);//更换(*pphead)->prev = NULL;return;
}void DLPopBack(DL** pphead)
{assert(pphead);assert(*pphead);DL* tail = *pphead;//而且还要找到倒数第二个节点,//改变其的next  NULL//但如果只有一个节点if (tail->next == NULL){free(pphead);
//		*pphead = NULL;return;}while (tail->next->next)//找到最后二个节点{tail = tail->next;}DL* back = tail->next;//找到最后一个节点free(back);tail->next = NULL;//修改倒数第二个节点return;
}DL* DListFind(DL* plist, SLdatatype x)
{assert(plist);DL* tail = plist;while (tail){if (tail->val == x)//判断{return tail;}//迭代tail = tail->next;}printf("该信息在该链表内不存在\n");return NULL;
}void DLEarse(DL** pphead, DL* pos)
{assert(pphead);assert(*pphead);assert(pos);DL* tail = *pphead;//但是还需要找到下一个节点  修改prev   改为上一节点//上一节点   修改next   改为下一节点while (tail != pos)//找到对应要删除的节点{tail = tail->next;}if (tail->next==NULL)//尾节点{DLPopBack(pphead);return;}else if (tail->prev == NULL)//头节点{DLPopFront(pphead);return;}DL* Front = tail->prev;DL* Back = tail->next;free(tail);Front->next = Back;Back->prev = Front;return;
}void DLInsertBack(DL** pphead, DL* pos, SLdatatype x)
{assert(pphead);assert(pos);DL* tail = *pphead;while (tail != pos)//找到对应节点{tail = tail->next;}if (tail->next == NULL)//尾节点{DLPushBack(pphead,x);return;}else if (tail->prev == NULL)//头节点{DLPushFront(pphead,x);return;}//先malloc//修改该节点的next  指向新添加的节点//修改本来节点的下一个节点   修改prev  改为指向新添加的节点DL* newcode = (DL*)malloc(sizeof(DL));if (newcode == NULL){perror("malloc error\n");}newcode->val = x;newcode->next = tail->next;newcode->prev = tail;DL* Back = tail->next;//原本链表下一个节点tail->next = newcode;//修改原本节点的next指向新添加的Back->prev = newcode;//修改原本节点下一个节点的prevreturn;
}void DLInsertFront(DL** pphead, DL* pos, SLdatatype x)//前插
{assert(pphead);assert(pos);DL* tail = *pphead;while (tail != pos)//找到对应节点{tail = tail->next;}if (tail->next == NULL)//尾节点{DLPushBack(pphead, x);return;}else if (tail->prev == NULL)//头节点{DLPushFront(pphead, x);return;}//先malloc//修改该节点的next  指向新添加的节点//修改本来节点的下一个节点   修改prev  改为指向新添加的节点DL* newcode = (DL*)malloc(sizeof(DL));if (newcode == NULL){perror("malloc error\n");}newcode->val = x;newcode->next = tail;newcode->prev = tail->prev;DL* Front = tail->prev;//原本链表下一个节点tail->prev = newcode;//修改原本节点的prev指向新添加的Front->next = newcode;//修改原本节点上一个节点的nextreturn;
}void Modify(DL** pphead, DL* pos, SLdatatype x)
{assert(pphead);assert(pos);DL* tail = *pphead;while (tail != pos)//找到对应节点{tail = tail->next;}tail->val = x;return;
}void DLprint(DL* pphead)
{DL* tail = pphead;while (tail != NULL){printf("%d->", tail->val);tail = tail->next;}printf("NULL\n");
}void DListDestory(DL** pphead)
{DL* tmp = *pphead;while (*pphead != NULL){//先保存该节点指向的地址tmp = *pphead;*pphead = (*pphead)->next;free(tmp);tmp = NULL;}return;
}

 DLList.h

#define  _CRT_SECURE_NO_WARNINGS#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>typedef int SLdatatype;typedef struct ListNode
{SLdatatype val;struct ListNode* next;struct ListNode* prev;
}DL;void DLPushFront(DL** pphead, SLdatatype x);void DLPushBack(DL** pphead, SLdatatype x);void DLPopFront(DL** pphead);void DLPopBack(DL** pphead);DL* DListFind(DL* plist, SLdatatype x);void DLEarse(DL** pphead,DL* pos);void DLInsertBack(DL** pphead, DL* pos, SLdatatype x);void DLInsertFront(DL** pphead, DL* pos, SLdatatype x);void Modify(DL** pphead, DL* pos, SLdatatype x);void DLprint(DL* pphead);void DListDestory(DL** pphead);

相关文章:

双向链表代码(带哨兵位循环/不带哨兵位不循环

以下代码全为本人所写&#xff0c;如有错误&#xff0c;很正常&#xff0c;还请指出&#xff0c; 目录 带哨兵位循环 test.c DLList.c DLList.h 不带哨兵位不循环 test.c DLList.c DLList.h 带哨兵位循环 test.c #define _CRT_SECURE_NO_WARNINGS#include"DLlist.h&…...

C语言自学笔记13----C语言指针与函数

C 语言指针与函数 在C语言编程中&#xff0c;也可以将地址作为参数传递给函数。 要在函数定义中接受这些地址&#xff0c;我们可以使用指针。这是因为指针用于存储地址。让我们举个实例&#xff1a; 示例&#xff1a;通过引用致电 #include <stdio.h> void swap(int n1, …...

每日五道java面试题之mybatis篇(一)

目录&#xff1a; 第一题. MyBatis是什么&#xff1f;第二题. ORM是什么?第三题. 为什么说Mybatis是半自动ORM映射工具&#xff1f;它与全自动的区别在哪里&#xff1f;第四题. 传统JDBC开发存在的问题第五题. JDBC编程有哪些不足之处&#xff0c;MyBatis是如何解决这些问题的…...

一文解读ISO26262安全标准:概念阶段

一文解读ISO26262安全标准&#xff1a;概念阶段 1 相关项定义2 安全生命周期启动3 危害分析和风险评估 HaRa4 功能安全概念 由上一篇文章知道&#xff0c;安全生命周期包含概念阶段、产品开发阶段、生产发布后续阶段。本文详细解读概念阶段要进行的安全活动。 本部分规定了车辆…...

微信小程序调用百度智能云API(菜品识别)

一、注册后生成应用列表创建应用 二、找到当前所需使用的api菜品识别文档 三、点链接看实例代码 这里需要使用到如下几个参数&#xff08;如下&#xff09;&#xff0c;其他的参数可以不管 client_id &#xff1a; 就是创建应用后的API Keyclient_secret&#xff1a; 就是创建…...

idea项目mapper.xml中的SQL语句黄色下划线去除

问题描述 当我们使用idea开发java项目时&#xff0c;经常会与数据库打交道&#xff0c;一般在使用mybatis的时候需要写一大堆的mapper.xml以及SQL语句&#xff0c;每当写完SQL语句的时候总是有黄色下划线&#xff0c;看着很不舒服。 解决方案&#xff1a; 修改idea的配置 Edi…...

es 聚合操作(二)

书接上文&#xff0c;示例数据在上一篇&#xff0c;这里就不展示了 一、Pipeline Aggregation 支持对聚合分析的结果&#xff0c;再次进行聚合分析。 Pipeline 的分析结果会输出到原结果中&#xff0c;根据位置的不同&#xff0c;分为两类&#xff1a; Sibling - 结果和现有…...

【vue.js】文档解读【day 5】| ref模板引用

如果阅读有疑问的话&#xff0c;欢迎评论或私信&#xff01;&#xff01; 本人会很热心的阐述自己的想法&#xff01;谢谢&#xff01;&#xff01;&#xff01; 文章目录 模板引用前言访问模板引用模板引用与v-if、v-show的结合v-for中的模板引用函数模板引用 模板引用 前言 …...

算法简单小技巧

主页&#xff1a;xiaocr_blog 1.最小公倍数和最大公约数 #include<iostream> using namespace std; int main(){int a,b;cin>>a>>b;int r a%b;while (r!0){a b;b r;r a%b;}cout<<b<<endl;return 0 ; } #include<iostream> using nam…...

前端入职配置新电脑!!!

前端岗位入职第一天到底应该做些什么呢&#xff1f;又该怎样高效的认识、融入团队&#xff1f;并快速进入工作状态呢&#xff1f;这篇文章就来分享一下&#xff0c;希望对即将走向或初入前端职场的你&#xff0c;能够有所帮助。内含大量链接&#xff0c;欢迎点赞收藏&#xff0…...

Java面试题总结15之简述你对RPC,RMI的理解

RPC&#xff1a;在本地调用远程的函数&#xff0c;远程过程调用&#xff0c;可以跨语言实现&#xff0c;httpClient RMI&#xff1a;远程方法调用&#xff0c;Java中用于实现RPC的一种机制&#xff0c;RPC的Java版本是J2EE的网络调用机制&#xff0c;跨JVM调用对象的方法&…...

内网穿透利器 n2n 搭建指南

1. n2n 简介 上文实验分析了 FRP 和 Zerotier 的利弊&#xff0c;本文再介绍另一种内网穿透方案&#xff0c;n2n。 n2n 是 C/S 架构的内网穿透服务&#xff0c;不同于 FRP 的 反向代理&#xff0c;它的原理是类似 Zerotier 的先打孔&#xff0c;打孔失败再尝试转发。关于打孔本…...

phpcms头像上传漏洞引发的故事

目录 关键代码 第一次防御 第一次绕过 第二次防御 第二次绕过 第三次防御 第三次绕过 如何构造一个出错的压缩包 第四次防御 第四次绕过 本篇文章是参考某位大佬与开发人员对于文件包含漏洞的较量记录下的故事&#xff0c;因为要学习文件包含漏洞&#xff0c;就将大佬…...

二叉树|二叉树理论基础、二叉树的递归遍历

代码随想录 (programmercarl.com) 树和二叉树 1.树的基本概念 1.1树的定义 1.2树的逻辑表示方法 1.3树的基本术语 1.4树的性质 1.5树的基本运算 1.6树的存储结构 2.二叉树的概念和性质 2.1二叉树的定义 2.2二叉树的性质 2.3二叉树与树、森林之间的转换 3.二叉树的…...

JavaScript 语法-对象

对象 JavaScript 中的对象是一组键值对的集合&#xff0c;其中每个键都是字符串&#xff0c;每个值可以是任意类型。 对象是由一些属性和方法组成的集合&#xff0c;属性可以用来存储数据&#xff0c;方法可以用来操作数据。 属性和方法使用“.”来访问 // 创建一个对象 let …...

代码随想录阅读笔记-哈希表【四数之和】

题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target&#xff0c;判断 nums 中是否存在四个元素 a&#xff0c;b&#xff0c;c 和 d &#xff0c;使得 a b c d 的值与 target 相等&#xff1f;找出所有满足条件且不重复的四元组。 注意&#xff1a;答案中不可以包…...

JVM学习——双亲委派机制

简而言之就是为了防止与Java固有全类名重复&#xff0c;而导致系统崩坏所设立的机制。 当类加载器接收到加载类的任务时&#xff0c;首先会向上请求&#xff0c;一直请求到引导类加载器&#xff0c;如果引导类加载器无法加载&#xff0c;就会逐层返回让类加载器自己执行&#…...

【Paper Reading】6.RLHF-V 提出用RLHF的1.4k的数据微调显著降低MLLM的虚幻问题

分类 内容 论文题目 RLHF-V: Towards Trustworthy MLLMs via Behavior Alignment from Fine-grained Correctional Human Feedback 作者 作者团队&#xff1a;由来自清华大学和新加坡国立大学的研究者组成&#xff0c;包括Tianyu Yu, Yuan Yao, Haoye Zhang, Taiwen He, Y…...

Aloudata 倾力打造,《Data Fabric 白皮书 2.0》正式发布

数字经济时代&#xff0c;越来越多企业开始寻求全新的数据管理范式&#xff0c;以更有效地管理、利用不断增长的数据资产。在此背景下&#xff0c;Data Fabric 的概念应运而生&#xff0c;被视为面向未来的数据管理解决方案。 距离第一版白皮书问世已经过去一年多时间&#xff…...

docker内部无法使用ping等网络工具解决方案

通常docker内部没有网络&#xff0c;所以我们先离线安装需要的依赖包&#xff0c;然后再使用sh脚本容器内部访问宿主机同网络端其他服务器ip,实现监测远程ip telnet包依赖于netbase包&#xff0c;但是netbase包没有安装。你需要先安装netbase包&#xff0c;然后再尝试安装teln…...

基于Flask实现的医疗保险欺诈识别监测模型

基于Flask实现的医疗保险欺诈识别监测模型 项目截图 项目简介 社会医疗保险是国家通过立法形式强制实施&#xff0c;由雇主和个人按一定比例缴纳保险费&#xff0c;建立社会医疗保险基金&#xff0c;支付雇员医疗费用的一种医疗保险制度&#xff0c; 它是促进社会文明和进步的…...

对WWDC 2025 Keynote 内容的预测

借助我们以往对苹果公司发展路径的深入研究经验&#xff0c;以及大语言模型的分析能力&#xff0c;我们系统梳理了多年来苹果 WWDC 主题演讲的规律。在 WWDC 2025 即将揭幕之际&#xff0c;我们让 ChatGPT 对今年的 Keynote 内容进行了一个初步预测&#xff0c;聊作存档。等到明…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面

代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口&#xff08;适配服务端返回 Token&#xff09; export const login async (code, avatar) > {const res await http…...

全志A40i android7.1 调试信息打印串口由uart0改为uart3

一&#xff0c;概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本&#xff1a;2014.07&#xff1b; Kernel版本&#xff1a;Linux-3.10&#xff1b; 二&#xff0c;Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01)&#xff0c;并让boo…...

html css js网页制作成品——HTML+CSS榴莲商城网页设计(4页)附源码

目录 一、&#x1f468;‍&#x1f393;网站题目 二、✍️网站描述 三、&#x1f4da;网站介绍 四、&#x1f310;网站效果 五、&#x1fa93; 代码实现 &#x1f9f1;HTML 六、&#x1f947; 如何让学习不再盲目 七、&#x1f381;更多干货 一、&#x1f468;‍&#x1f…...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同&#xff0c;结合所安装的tensorflow的目录结构修改from语句即可。 原语句&#xff1a; from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后&#xff1a; from tensorflow.python.keras.lay…...

push [特殊字符] present

push &#x1f19a; present 前言present和dismiss特点代码演示 push和pop特点代码演示 前言 在 iOS 开发中&#xff0c;push 和 present 是两种不同的视图控制器切换方式&#xff0c;它们有着显著的区别。 present和dismiss 特点 在当前控制器上方新建视图层级需要手动调用…...

从 GreenPlum 到镜舟数据库:杭银消费金融湖仓一体转型实践

作者&#xff1a;吴岐诗&#xff0c;杭银消费金融大数据应用开发工程师 本文整理自杭银消费金融大数据应用开发工程师在StarRocks Summit Asia 2024的分享 引言&#xff1a;融合数据湖与数仓的创新之路 在数字金融时代&#xff0c;数据已成为金融机构的核心竞争力。杭银消费金…...

日常一水C

多态 言简意赅&#xff1a;就是一个对象面对同一事件时做出的不同反应 而之前的继承中说过&#xff0c;当子类和父类的函数名相同时&#xff0c;会隐藏父类的同名函数转而调用子类的同名函数&#xff0c;如果要调用父类的同名函数&#xff0c;那么就需要对父类进行引用&#…...

Vue3中的computer和watch

computed的写法 在页面中 <div>{{ calcNumber }}</div>script中 写法1 常用 import { computed, ref } from vue; let price ref(100);const priceAdd () > { //函数方法 price 1price.value ; }//计算属性 let calcNumber computed(() > {return ${p…...