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

【双向链表】

双向链表

  • 带头双向循环链表的实现
    • 1. 函数的声明
    • 2. 函数的实现
    • 3. 主函数测试

带头双向循环链表的实现

今天我们来实现一下带头双向循环链表,顾名思义,带头就是有哨兵位,哨兵位不是链表的头,它是连接头节点的一个节点,方便操作链表而已;双向就是一个节点可以找到下一个节点,也可以找到上一个节点,所以每个节点应该有两个结构体指针;循环就是没有空指针,哨兵位的上一个节点是尾,尾的下一个节点是哨兵位;如下图为带头双向循环链表:

在这里插入图片描述

1. 函数的声明

以下是函数的声明部分,我们主要实现双向链表的增删查改功能;

		#pragma once#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <stdbool.h>//类型的命名typedef int LTDataType;//定义节点typedef struct ListNode{struct ListNode* prev;struct ListNode* next;LTDataType data;}LTNode;//初始化链表LTNode* ListInit();//打印链表void ListPrint(LTNode* phead);//判断是否是空链表bool IsEmpty(LTNode* phead);//尾插、头插、头删、尾删void ListPushBack(LTNode* phead, LTDataType x);void ListPushFront(LTNode* phead, LTDataType x);void ListPopFront(LTNode* phead);void ListPopBack(LTNode* phead);//查找LTNode* LTFindPos(LTNode* phead, LTDataType x);//在pos位置之前插入void LTInsertPos(LTNode* pos, LTDataType x);//删除pos位置void LTErasePos(LTNode* pos);//销毁void LTDestroy(LTNode* phead);

2. 函数的实现

为了防止创建新的节点的时候重复出现,我们将创建节点写成一个函数:

		LTNode* BuyListNode(int x){LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));assert(newnode);newnode->data = x;newnode->next = NULL;newnode->prev = NULL;return newnode;}

初始化节点,只需要一个哨兵位,它的next和prev都指向自己;

		LTNode* ListInit(){LTNode* phead = BuyListNode(-1);phead->next = phead;phead->prev = phead;return phead;}

打印链表的函数:

		void ListPrint(LTNode* phead){assert(phead);LTNode* cur = phead->next;printf("guard<==>");while (cur != phead){printf("%d<==>",cur->data);cur = cur->next;}printf("\n");}

由于头删尾删的时候不能是空链表,带头的链表的空链表相当于只有一个哨兵位,所以头删尾删的时候链表中不能只剩下哨兵位;

		bool IsEmpty(LTNode* phead){assert(phead);return phead->next == phead;}

尾插函数:

		void ListPushBack(LTNode* phead,LTDataType x){assert(phead);LTNode* newnode = BuyListNode(x);LTNode* tail = phead->prev;newnode->next = phead;tail->next = newnode;newnode->prev = tail;phead->prev = newnode;}

头插函数:

		void ListPushFront(LTNode* phead, LTDataType x){assert(phead);LTNode* newnode = BuyListNode(x);LTNode* next = phead->next;newnode->next = next;newnode->prev = phead;next->prev = newnode;phead->next = newnode;}

头删函数:

		void ListPopFront(LTNode* phead){assert(phead);assert(!IsEmpty(phead));LTNode* del = phead->next;LTNode* next = del->next;phead->next = next;next->prev = phead;free(del);}

尾删函数:

		void ListPopBack(LTNode* phead){assert(phead);assert(!IsEmpty(phead));LTNode* tail = phead->prev;LTNode* tailprev = tail->prev;tailprev->next = phead;phead->prev = tailprev;free(tail);}

查找某个节点的函数,返回找到的节点,否则返回空;

		LTNode* LTFindPos(LTNode* phead, LTDataType x){assert(phead);LTNode* cur = phead->next;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;}

在pos位置之前插入的插入函数:

		void LTInsertPos(LTNode* pos, LTDataType x){assert(pos);LTNode* newnode = BuyListNode(x);LTNode* ptr = pos->prev;ptr->next = newnode;newnode->next = pos;pos->prev = newnode;newnode->prev = ptr;}

删除pos位置的函数:

		void LTErasePos(LTNode* pos){assert(pos);assert(!IsEmpty(pos));LTNode* ptr = pos->prev;LTNode* next = pos->next;ptr->next = next;next->prev = ptr;free(pos);}

销毁链表的函数:

		void LTDestroy(LTNode* phead){assert(phead);LTNode* cur = phead->next;while (cur != phead){LTNode* next = cur->next;free(cur);cur = next;}free(phead);}

3. 主函数测试

		#include "List.h"int main(){LTNode* phead = ListInit();ListPushBack(phead, 1);ListPushBack(phead, 2);ListPushBack(phead, 3);ListPushBack(phead, 4);ListPushFront(phead, 10);ListPopBack(phead);ListPopFront(phead);LTNode* pos = LTFindPos(phead, 3);LTInsertPos(pos, 20);LTErasePos(pos);ListPrint(phead);LTDestroy(phead);return 0;}

以上就是带头双向循环链表的基本实现,感兴趣的伙伴可以自行尝试噢!!!

http://www.lryc.cn/news/92948.html

相关文章:

  • POSTGRESQL NEON - Serverless 式的POSTGRESQL 数据库的独特技能 分支数据
  • 数据分布——长尾分布的处理
  • 集合导题、刷题、考试全套完整流程,专业强大的功能,提高刷题学习效率和企业的培训效率
  • 【机器学习】采样方法
  • Seata TCC 模式理论学习、生产级使用示例搭建及注意事项 | Spring Cloud55
  • 一文详解:Vue3中使用Vue Router
  • C++开发—远程控制
  • 【Python基础】Python数据容器(集合)
  • 高通 Camera HAL3:集成camxoverridesettings.txt到整机版本
  • PHP面试题大全
  • Linux发送接收邮件
  • SpringBoot-【回顾】
  • Python模拟试卷2023(1)
  • 常量接口 vs 常量类 vs 枚举区别
  • 第二章 模态命题:必然、可能
  • Selenium 必了解—如何测试REST API
  • pytorch安装老版本
  • 怎么自学电脑编程
  • 【华为OD统一考试B卷 | 100分】斗地主之顺子(C++ Java JavaScript Python)
  • 案例39:基于Java办公自动化管理系统开题报告设计
  • 基于山景BP10128音频处理器高通滤波器算法设计
  • docker搭建本地私有仓库
  • Asp.net某店POS积分管理系统-清除履历表、日志表、月购买额(源代码+论文)
  • Baumer工业相机堡盟工业相机如何使用BGAPISDK的相机图像时间戳计算运行时间以及时间差(C#)
  • python:消除已安装库在import导入时出现红线问题
  • 关闭nginx容器之后,再次启动,原来宿主机映射的端口失效的问题解决
  • 【小沐学Python】Python实现在线电子书(MkDocs + readthedocs + github + Markdown)
  • Python 中的短路评估
  • LVGL源码分析(1):lv_ll链表的实现
  • js判断数据类型的几种方法及其局限性(typeof, instanceof, Object.prototype.toString.call())