实现带头双向循环链表

🌈带头双向循环链表

在这里插入图片描述
描述:一个节点内包含两个指针,一个指向上一个节点,另一个指向下一个节点。哨兵位指向的下一个节点为头节点,哨兵位的上一个指向尾节点。
结构优势:高效率找尾节点;高效率插入与删除;无需判断多种复杂情况,如尾节点、空节点等。

🌈实现带头双向循环链表

☀️list.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int DataType;
typedef struct ListNode {struct ListNode* prev;struct ListNode* next;DataType data;
}ListNode;ListNode* BuyListNode(DataType x);
ListNode* InitList();
void DestroyList(ListNode* phead);void Print(ListNode* phead);
int CountSize(ListNode* phead);void PushBack1(ListNode* phead, DataType x);
void PushBack2(ListNode* phead, DataType x);void PopBack1(ListNode* phead);
void PopBack2(ListNode* phead);void PushFront1(ListNode* phead, DataType x);
void PushFront2(ListNode* phead, DataType x);void PopFront1(ListNode* phead);
void PopFront2(ListNode* phead);void Insert(ListNode* pos, DataType x);
void Erase(ListNode* pos);

☀️list.c

BuyListNode节点创建函数()

ListNode* BuyListNode(DataType x) {ListNode* node = (ListNode*)malloc(sizeof(ListNode));if (node == NULL) {perror("malloc fail");exit(-1);}node->data = x;node->prev = NULL;node->next = NULL;return node;
}

InitList链表初始化函数()

ListNode* InitList() {ListNode* phead = BuyListNode(0);phead->next = phead;phead->prev = phead;return phead;
}

DestroyList链表销毁函数()

void DestroyList(ListNode* phead) {assert(phead);ListNode* cur = phead->next;while (cur != phead) {ListNode* curnext = cur->next;free(cur);cur = curnext;}free(phead);
}

打印节点信息函数Print()

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

统计节点个数函数CountSize()

int CountSize(ListNode* phead) {assert(phead);int size = 0;ListNode* cur = phead->next;while (cur != phead) {size++;cur = cur->next;}return size;
}

在pos位置节点前插入函数Insert()

//在pos前插入
void Insert(ListNode* pos, DataType x) {assert(pos);ListNode* posprev = pos->prev;ListNode* newnode = BuyListNode(x);posprev->next = newnode;newnode->prev = posprev;newnode->next = pos;pos->prev = newnode;
}

删除pos位置节点函数Erase()

void Erase(ListNode* pos) {assert(pos);ListNode* posprev = pos->prev;ListNode* posnext = pos->next;free(pos);posprev->next = posnext;posnext->prev = posprev;
}

尾插(两种方法)PushBack1()&PushBack2()

void PushBack1(ListNode* phead, DataType x) {ListNode* tail = phead->prev;ListNode* newnode = BuyListNode(x);newnode->next = phead;phead->prev = newnode;tail->next = newnode;newnode->prev = tail;
}
void PushBack2(ListNode* phead, DataType x) {//尾插就相当于在哨兵位head前插入Insert(phead, x);
}

尾删(两种方法)PopBack1()&PopBack2()

void PopBack1(ListNode* phead) {assert(phead);assert(phead->next != phead);ListNode* tail = phead->prev;ListNode* tailprev = tail->prev;free(tail);tailprev->next = phead;phead->prev = tailprev;
}
void PopBack2(ListNode* phead) {//尾节点就是phead的prev节点Erase(phead->prev);
}

头插(两种方法)PushFront1()&PushFront2()

void PushFront1(ListNode* phead, DataType x) {assert(phead);ListNode* newnode = BuyListNode(x);ListNode* pheadnext = phead->next;newnode->next = pheadnext;pheadnext->prev = newnode;phead->next = newnode;newnode->prev = phead;
}
void PushFront2(ListNode* phead, DataType x) {//头插就相当于在phead后一个节点的前面插入assert(phead);Insert(phead->next, x);
}

头删(两种方法)PopFront1()&PopFront2()

void PopFront1(ListNode* phead) {assert(phead);assert(phead->next != phead);ListNode* first = phead->next;ListNode* second = first->next;free(first);phead->next = second;second->prev = phead;
}
void PopFront2(ListNode* phead) {//头节点时哨兵位phead的下一个节点Erase(phead->next);
}

☀️测试

测试尾插:test_PushBack(()

#define _CRT_SECURE_NO_WARNINGS
#include"list.h"
void test_PushBack() {ListNode* plist = InitList();PushBack1(plist, 1);PushBack1(plist, 2);PushBack1(plist, 3);PushBack2(plist, 1);PushBack2(plist, 2);PushBack2(plist, 3);Print(plist);DestroyList(plist);
}

测试结果:
在这里插入图片描述

测试尾删:test_PopBack()

void test_PopBack() {ListNode* plist = InitList();PushBack1(plist, 1);PushBack1(plist, 2);PushBack1(plist, 3);PushBack2(plist, 1);PushBack2(plist, 2);PushBack2(plist, 3);Print(plist);PopBack1(plist);Print(plist);PopBack2(plist);Print(plist);DestroyList(plist);
}

测试结果:
在这里插入图片描述

测试头插:test_PushFront()

void test_PushFront() {ListNode* plist = InitList();PushFront1(plist, 1);PushFront1(plist, 2);PushFront1(plist, 3);PushFront2(plist, 1);PushFront2(plist, 2);PushFront2(plist, 3);Print(plist);DestroyList(plist);
}

测试结果:
在这里插入图片描述

测试头删:test_PopFront()

void test_PopFront() {ListNode* plist = InitList();PushFront1(plist, 1);PushFront1(plist, 2);PushFront1(plist, 3);PushFront2(plist, 1);PushFront2(plist, 2);PushFront2(plist, 3);Print(plist);PopFront1(plist);Print(plist);PopFront2(plist);Print(plist);DestroyList(plist);
}

测试结果:
在这里插入图片描述

测试用主函数

int main() {//测试尾插test_PushBack();//测试尾删test_PopBack();//测试头插test_PushFront();//测试头删test_PopFront();
}

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

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

相关文章

【LeetCode-面试经典150题-day14】

目录 19.删除链表的倒数第N个结点 82.删除排序链表中的重复元素Ⅱ 61. 旋转链表 86.分隔链表 146.LRU缓存 19.删除链表的倒数第N个结点 题意&#xff1a; 给你一个链表&#xff0c;删除链表的倒数第 n 个结点&#xff0c;并且返回链表的头结点。 【输入样例】head [1,2,3,4,5…

大数据之linux入门

一、linux是什么 linux操作系统 开发者是林纳斯-托瓦兹&#xff0c;出于个人爱好编写。linux是一个基于posix和unix的多用户、多任务、支持多线程和多CPU的操作系统。 Unix是20世纪70年代初出现的一个操作系统&#xff0c;除了作为网络操作系统之外&#xff0c;还可以作为单…

论文阅读_扩散模型_LDM

英文名称: High-Resolution Image Synthesis with Latent Diffusion Models 中文名称: 使用潜空间扩散模型合成高分辨率图像 地址: https://ieeexplore.ieee.org/document/9878449/ 代码: https://github.com/CompVis/latent-diffusion 作者&#xff1a;Robin Rombach 日期: 20…

基于ADAU1452 DSP ANC和AEC算法的实现

是否需要申请加入数字音频系统研究开发交流答疑群(课题组)?加我微信hezkz17, 本群提供音频技术答疑服务,+群附加赠送,DSP音频项目核心开发资料, 1 使用Sigma中的NLMS算法模块 对应C源代码:

Web开发模式、API接口、restful规范、序列化和反序列化、drf安装和快速使用、路由转换器(复习)

一 Web开发模式 1. 前后端混合开发模式 前后端混合开发模式是一种开发方式&#xff0c;将前端和后端的开发工作结合在一起&#xff0c;以加快项目的开发速度和 提高协作效率。这种模式通常用于快速原型开发、小型项目或敏捷开发中。在前后端混合开发模式中&#xff0c;前端和…

Rust之自动化测试(一):如何编写测试

开发环境 Windows 10Rust 1.71.1 VS Code 1.81.1 项目工程 这里继续沿用上次工程rust-demo 编写自动化测试 Edsger W. Dijkstra在他1972年的文章《谦逊的程序员》中说&#xff0c;“程序测试可以是一种非常有效的方法来显示错误的存在&#xff0c;但它对于显示它们的不存在…

视频分割合并工具说明

使用说明书&#xff1a;视频分割合并工具 欢迎使用视频生成工具&#xff01;本工具旨在帮助您将视频文件按照指定的规则分割并合并&#xff0c;以生成您所需的视频。 本程序还自带提高分辨率1920:1080&#xff0c;以及增加10db声音的功能 软件下载地址 https://github.com/c…

Fei-Fei Li-Lecture 16:3D Vision 【斯坦福大学李飞飞CV课程第16讲:3D Vision】

目录 P1 2D Detection and Segmentation P2 Video 2D time series P3 Focus on Two Problems P4 Many more topics in 3D Vision P5-10 Multi-View CNN P11 Experiments – Classification & Retrieval P12 3D Shape Representations P13--17 3D Shape Represen…

echarts 甘特图一组显示多组数据

<template><el-button type"primary" click"addlin">添加线</el-button><el-button type"success" click"addArea">添加区域</el-button><div ref"echart" id"echart" class&qu…

应急物资管理系统|智物资DW-S300提升应急响应能力

项目背景 智慧应急物资管理系统&#xff08;智装备DW-S300&#xff09;是一套成熟系统&#xff0c;依托互3D技术、云计算、大数据、RFID技术、数据库技术、AI、视频分析技术对RFID智能仓库进行统一管理、分析的信息化、智能化、规范化的系统。 本项目采用东识智慧应急物资管理…

(三)行为模式:6、备忘录模式(Memento Pattern)(C++示例)

目录 1、备忘录模式&#xff08;Memento Pattern&#xff09;含义 2、备忘录模式的UML图学习 3、备忘录模式的应用场景 4、备忘录模式的优缺点 &#xff08;1&#xff09;优点&#xff1a; &#xff08;2&#xff09;缺点 5、C实现备忘录模式的实例 1、备忘录模式&#…

javaee spring 自动注入,如果满足条件的类有多个如何区别

如图IDrinkDao有两个实现类 方法一 方法二 Resource(name“对象名”) Resource(name"oracleDrinkDao") private IDrinkDao drinkDao;