双向带头链表实现

目录

一. 逻辑结构图解

1. 节点中存储的值

 2.逻辑实现

二. 各种功能实现

1. 创建节点函数

2. 初始化哨兵位

3. 尾插

4. 头插

5. 尾删

6. 头删

7. 打印链表值

8. 查找数据,返回节点地址

9. 指定地址后插入节点

10. 删除指定地址节点

11. 销毁链表

三. 完整代码

1. list.h

2. list.c

3. 测试


由于上一篇已经对链表的基本概念讲解完毕,这里就不过多赘述了

一. 逻辑结构图解

1. 节点中存储的值

qrev是上一个节点的地址

data是节点中存储的数据

next是下一个节点的位置

 2.逻辑实现

 第一个节点为哨兵位不存储数据

二. 各种功能实现

1. 创建节点函数
ListNode* BuyListNode(DataType x)
{ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));if (newnode == NULL){exit(-1);}newnode->data = x;newnode->next = newnode->prev = newnode;return newnode;
}
2. 初始化哨兵位

由于需要改变哨兵位本身,所以用二级指针

之后就不用改变哨兵位了所以不用用二级指针

void ListNodeInit(ListNode** pphead)
{*pphead = BuyListNode(0);
}
3. 尾插
void ListNodePushBack(ListNode* phead, DataType x)
{ListNode* tail = phead->prev;assert(phead);ListNode* newnode = BuyListNode(x);newnode->prev = phead->prev;newnode->next = phead;tail->next = newnode;//之前的尾指向现在的尾phead->prev = newnode;//头的上一个改为现在的尾
}
4. 头插
void ListNodePushFront(ListNode* head,DataType x)
{assert(head);ListNode* first = head->next;ListNode* newnode = BuyListNode(x);newnode->next = head->next;newnode->prev = head;first->prev = newnode;head->next = newnode;
}
5. 尾删
void ListNodePopBack(ListNode* head)
{assert(head);ListNode* tail=head->prev;ListNode* newtail = tail->prev;head->prev = newtail;newtail->next = head;free(tail);tail = NULL;
}
6. 头删
void ListNodePopFront(ListNode* head)
{assert(head);ListNode* first=head->next;ListNode* newfirst = first->next;head->next = newfirst;newfirst->prev = head;free(first);first = NULL;
}
7. 打印链表值
void ListNodePrint(ListNode* phead)
{assert(phead);ListNode* tmp = phead->next;while (tmp!= phead){printf("%d  ", tmp->data);tmp = tmp->next;}
}
8. 查找数据,返回节点地址
ListNode* ListNodeFind(ListNode* head,DataType x)
{assert(head);ListNode* tmp = head->next;while (tmp!=head){if (tmp->data == x)return tmp;tmp = tmp->next;}return NULL;
}
9. 指定地址后插入节点
void ListNodeInsert(ListNode* pos, DataType x)
{assert(pos);ListNode* newnext = pos->next;ListNode* newnode = BuyListNode(x);newnode->prev = pos;newnode->next = newnext;pos->next = newnode;newnext->prev = newnode;}
10. 删除指定地址节点
void ListNodeErase(ListNode* pos)
{assert(pos);ListNode* posprev = pos->prev, * posnext = pos->next;posprev->next = posnext;posnext->prev = posprev;free(pos);pos = NULL;
}
11. 销毁链表
void ListNodeDestroy(ListNode* head)
{assert(head);ListNode* cur = head->next;while (cur != head){ListNode* next = cur->next;free(cur);cur = next;}
}

三. 完整代码

1. list.h
#define _CRT_SECURE_NO_WARNINGS h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int DataType;typedef struct ListNode
{DataType data;struct SList* next;struct SList* prev;
}ListNode;//初始化
void ListNodeInit(ListNode** pphead);
//尾插
void ListNodePushBack(ListNode* head, DataType x);
//打印链表
void ListNodePrint(ListNode* phead);
//头插
void ListNodePushFront(ListNode* head, DataType x);
//尾删
void ListNodePopBack(ListNode* head);
//头删
void ListNodePopFront(ListNode* head);
//查找数据
ListNode* ListNodeFind(ListNode* head, DataType x);
//指定位置后插入
void ListNodeInsert(ListNode* pos, DataType x);
//指定位置删除
void ListNodeErase(ListNode* pos);
//销毁链表
void ListNodeDestroy(ListNode* head);
2. list.c
#define _CRT_SECURE_NO_WARNINGS h#include"list.h"ListNode* BuyListNode(DataType x)
{ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));if (newnode == NULL){exit(-1);}newnode->data = x;newnode->next = newnode->prev = newnode;return newnode;
}void ListNodeInit(ListNode** pphead)
{*pphead = BuyListNode(0);
}void ListNodePushBack(ListNode* phead, DataType x)
{ListNode* tail = phead->prev;assert(phead);ListNode* newnode = BuyListNode(x);newnode->prev = phead->prev;newnode->next = phead;tail->next = newnode;//之前的尾指向现在的尾phead->prev = newnode;//头的上一个改为现在的尾
}void ListNodePrint(ListNode* phead)
{assert(phead);ListNode* tmp = phead->next;while (tmp!= phead){printf("%d  ", tmp->data);tmp = tmp->next;}
}void ListNodePushFront(ListNode* head,DataType x)
{assert(head);ListNode* first = head->next;ListNode* newnode = BuyListNode(x);newnode->next = head->next;newnode->prev = head;first->prev = newnode;head->next = newnode;
}void ListNodePopBack(ListNode* head)
{assert(head);ListNode* tail=head->prev;ListNode* newtail = tail->prev;head->prev = newtail;newtail->next = head;free(tail);tail = NULL;
}void ListNodePopFront(ListNode* head)
{assert(head);ListNode* first=head->next;ListNode* newfirst = first->next;head->next = newfirst;newfirst->prev = head;free(first);first = NULL;
}ListNode* ListNodeFind(ListNode* head,DataType x)
{assert(head);ListNode* tmp = head->next;while (tmp!=head){if (tmp->data == x)return tmp;tmp = tmp->next;}return NULL;
}void ListNodeInsert(ListNode* pos, DataType x)
{assert(pos);ListNode* newnext = pos->next;ListNode* newnode = BuyListNode(x);newnode->prev = pos;newnode->next = newnext;pos->next = newnode;newnext->prev = newnode;}void ListNodeErase(ListNode* pos)
{assert(pos);ListNode* posprev = pos->prev, * posnext = pos->next;posprev->next = posnext;posnext->prev = posprev;free(pos);pos = NULL;
}void ListNodeDestroy(ListNode* head)
{assert(head);ListNode* cur = head->next;while (cur != head){ListNode* next = cur->next;free(cur);cur = next;}
}
3. 测试
#define _CRT_SECURE_NO_WARNINGS h#include"list.h"
void test()
{ListNode* head=NULL;ListNodeInit(&head);ListNodePushBack(head, 2);ListNodePushBack(head, 45);ListNodePushBack(head, 33);ListNodePushFront(head, 22);ListNodePushFront(head, 66);ListNode* find=ListNodeFind(head,33);ListNodeInsert(find, 666);ListNodePrint(head);printf("\n");//ListNodePopBack(head);ListNodeErase(find);ListNodePopFront(head);ListNodePrint(head);}int main()
{test();
}

感谢大家观看,希望可以帮到您

(づ ̄3 ̄)づ╭❤~

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

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

相关文章

vue2 自定义ant design vue (组件) 的message内容 及 vue 定义公用方法流程

1 modules.js import { Button, message } from ant-design-vue; function downFile(){ alert(下载) } export default { // 成功提示 success(_this,content,size = 14) { // message.success({ // content : h => // _this.$createElement(span, [ // …

JAVA云HIS医院系统源码 云HIS运维平台源码 融合B/S版电子病历系统,支持电子病历四级,saas模式

JAVA云HIS医院系统源码 云HIS运维平台源码 融合B/S版电子病历系统&#xff0c;支持电子病历四级&#xff0c;saas模式 HIS系统就是医院信息管理系统&#xff0c;HIS系统是整个医院信息化的核心&#xff0c;门诊、住院、药房、药库等都是由HIS系统来承载起来的&#xff0c;所以…

【Spring】深入学习AOP编程思想的实现原理和优势

【切面编程】深入学习AOP编程思想的实现原理和优势 前言AOP的由来及AOP与代理的关系AOP的实现方式详解静态代理动态代理 AOP的应用场景记录日志权限控制数据库事务控制缓存处理异常处理监控管理 AOP的底层实现全流程解析Spring AOP的简介动态代理的实现原理Spring AOP的实现原理…

rockeylinux 搭建k8s 1.28.10

1.关闭防火墙 systemctl stop firewalld systemctl disable firewalld 2.关闭selinux # 临时禁用selinux # 将 SELinux 设置为 permissive 模式&#xff08;相当于将其禁用&#xff09; setenforce 0 sed -i s/^SELINUXenforcing$/SELINUXpermissive/ /etc/selinux/config 3.网…

tinyrenderer-渲染器着色

整理了代码&#xff0c;创建了一个相机类&#xff0c;控制镜头 class Camera { public:Camera(Vec3f cameraPos, Vec3f target, Vec3f up):cameraPos_(cameraPos), target_(target), up_(up) {}Matrix getView();Matrix getProjection(); private:Vec3f cameraPos_;Vec3f targ…

安装Chrome扩展程序来 一键禁用页面上的所有动画和过渡。有那些扩展程序推荐一下

要安装Chrome扩展程序来一键禁用页面上的所有动画和过渡&#xff0c;以下是一些推荐的扩展程序&#xff1a; Toggle CSS Animations and Transitions 功能&#xff1a;此扩展程序允许用户轻松地在网页上切换CSS动画和过渡的开启与关闭状态。使用方法&#xff1a;安装后&#x…

2024年区块链,物联网与信息技术国际会议(ICBITIT 2024)

2024年区块链&#xff0c;物联网与信息技术国际会议&#xff08;ICBITIT 2024&#xff09; 2024 International Conference on Blockchain, Internet of Things, and Information Technology 会议简介&#xff1a; 2024年区块链&#xff0c;物联网与信息技术国际会议&#xff…

ML307R OpenCPU UDP使用

一、UDP通信流程 二、示例 三、UDP通信代码 一、UDP通信流程 ML307R UDP 是使用LWIP的标准的通信,具体UDP流程可以自行百度 二、示例 实验目的:实现把接收的数据再发送到服务端 测试网址:UDP电脑端测试网址 因为是4G,所以必须用外网的 /* 测试前请先补充如下参数 */…

Flutter 中的 CupertinoTimePicker 小部件:全面指南

Flutter 中的 CupertinoTimePicker 小部件&#xff1a;全面指南 在 Flutter 中&#xff0c;CupertinoTimePicker 是 Cupertino 组件库中的一个 widget&#xff0c;它提供了一个 iOS 风格的时间和日期选择器。这个选择器允许用户通过滚动选择器来选择时间&#xff0c;非常适合需…

halcon3D学习备份

目录 xyz_to_object_model_3d 1 gen_object_model_3d_from_points 1 object_model_3d_to_xyz 2 segment_object_model_3d 2 prepare_object_model_3d 4 distance_object_model_3d 5 area_object_model_3d 7 project_object_model_3d 8 surface_normals_object_model_3d 9 sampl…

css样式,点击 箭头方向上下转换

实现效果&#xff1a; 点击切换箭头方向 实现代码 <divclass"modelPart"click"showClick"><div class"modelPart_left"><img:srcaidefalutIconclass"sNodeIcon"><div>{{ selectModel }}</div><div …

Scala的简单认识

Scala编程基础 小白的Scala学习笔记 2024/5/21 上午某一时刻 文章目录 Scala编程基础spark是用Scala开发出来的Scala的优点 打开idea 搜索scala&#xff0c;安装 如果不小心点了取消&#xff0c;或者没有上图的提示&#xff0c;就在依赖里面添加 spark是用Scala开发出来的 类比…

英语学习笔记21+23——Which book?/Which glasses?

Which book?/Which glasses? 哪本书&#xff1f;/哪些杯子&#xff1f; 词汇 Vocabulary give v. 给 搭配&#xff1a;Give me five! 击掌庆祝 用法&#xff1a;give 人 东西     give 东西 to 人    把……东西给某人 例句&#xff1a;把这些苹果给 Bobby.   …

元宇宙vr美术虚拟展馆激发更多文化认同和互鉴

科技引领创新潮流&#xff0c;借助前沿的Web3D技术&#xff0c;我们为用户打造了一个沉浸式的纯3D虚拟空间体验平台&#xff1a;元宇宙线上互动展厅。您只需通过网页即可轻松访问这个充满未来感的互动平台。 在这个独特的虚拟环境中&#xff0c;用户可以轻松创建个性化角色&…

缓存三问与缓存预热-如何预防缓存崩溃

一、缓存三剑客 &#xff08;图片来源&#xff1a;什么是缓存雪崩、击穿、穿透&#xff1f; | 小林coding&#xff09; 缓存穿透 (Cache Penetration) 又称"空缓存"指用户请求的数据在缓存和数据库中都不存在,导致每次请求都去查询数据库,给数据库带来巨大压力。解…

【深度学习】【NLP】词表,分词,嵌入

from transformers import AutoTokenizertokenizer AutoTokenizer.from_pretrained("prajjwal1/bert-tiny") tokenizer.save_pretrained("./bert-tiny/")input_string "Your input string here 我是中文" token_ids tokenizer.encode(input_s…

【PID算法详解】

PID算法 PID算法介绍用途pid数学表达式及其含义P算法D算法I算法 PID总结数学公式转换代码设计实际运用PID代码实现 PID算法介绍 PID控制器是一种广泛应用于工业控制系统的反馈控制器&#xff0c;它通过比例&#xff08;Proportional&#xff09;、积分&#xff08;Integral&am…

快写猪好用吗 #知识分享#笔记#学习方法

快写猪是一个非常好用的论文写作工具&#xff0c;它提供了强大的查重降重功能&#xff0c;帮助用户轻松完成论文写作任务。无论是在学术研究还是日常写作中&#xff0c;快写猪都能提供高效、准确的检测&#xff0c;确保文本的原创性和质量。 首先&#xff0c;快写猪的查重降重功…

朴素贝叶斯算法解析:从原理到实践

课程链接&#xff1a;AI小天才&#xff1a;让你轻松掌握机器学习 引言&#xff1a; 朴素贝叶斯&#xff08;Naive Bayes&#xff09;算法是一种简单而又高效的机器学习算法&#xff0c;在文本分类、垃圾邮件过滤、情感分析等领域有着广泛的应用。本文将深入介绍朴素贝叶斯算法的…

c 系统宏有多少

在C语言中&#xff0c;系统宏&#xff08;也称为预定义宏或内置宏&#xff09;的数量并不是固定的&#xff0c;因为它们取决于C标准、编译器以及可能的其他因素。然而&#xff0c;有一些常见的预定义宏是几乎所有C编译器都支持的。 以下是一些常见的C预定义宏&#xff1a; __…