实现双向循环链表 - 详解

news/2025/9/21 20:07:21/文章来源:https://www.cnblogs.com/lxjshuju/p/19104057

双向循环链表

本文章将展示zack实现双向循环链表的代码

// list.h
#pragma once
#include<stdio.h>#include<stdlib.h>#include<assert.h>#include<stdbool.h>typedef int data_type;typedef struct list_node{data_type data;struct list_node* next;struct list_node* prve;}list_node;list_node* buy_node(data_type x);void list_init(list_node** pphead);void list_print(list_node* phead);void list_push_back(list_node* phead, data_type x);void list_push_front(list_node* phead, data_type x);bool list_empty(list_node* phead);void list_pop_back(list_node* phead);void list_pop_front(list_node* phead);list_node* list_find(list_node* phead, data_type x);// 在指定位置之前插入void list_insert(list_node* pos, data_type x);void list_erase(list_node* pos);void list_destroy(list_node** pphead);
// list.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
list_node* buy_node(data_type x)
{
list_node* new_node = (list_node*)malloc(sizeof(list_node));
if (new_node == NULL)
{
perror("malloc fail");
exit(1);
}
new_node->data = x;
new_node->next = new_node->prve = new_node;
return new_node;
}
void list_init(list_node** pphead)
{
*pphead = buy_node(-1);
}
void list_print(list_node* phead)
{
list_node* pcur = phead->next;
while (pcur != phead)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("\n");
}
void list_push_back(list_node* phead, data_type x)
{
assert(phead);
list_node* new_node = buy_node(x);
new_node->next = phead;
new_node->prve = phead->prve;
phead->prve->next = new_node;
phead->prve = new_node;
}
void list_push_front(list_node* phead, data_type x)
{
assert(phead);
list_node* new_node = buy_node(x);
new_node->next = phead->next;
new_node->prve = phead;
phead->next->prve = new_node;
phead->next = new_node;
}
bool list_empty(list_node* phead)
{
assert(phead);
return phead->next == phead;
}
void list_pop_back(list_node* phead)
{
list_empty(phead);
assert(phead);
list_node* del = phead->prve;
phead->prve->prve->next = phead;
phead->prve = phead->prve->prve;
free(del);
del = NULL;
}
void list_pop_front(list_node* phead)
{
list_empty(phead);
assert(phead);
list_node* del = phead->next;
phead->next->next->prve = phead;
phead->next = phead->next->next;
free(del);
del = NULL;
}
list_node* list_find(list_node* phead, data_type x)
{
list_node* pcur = phead->next;
while (pcur != phead)
{
if (pcur->data == x)
return pcur;
pcur = pcur->next;
}
return NULL;
}
void list_insert(list_node* pos, data_type x)
{
assert(pos);
list_node* new_node = buy_node(x);
new_node->next = pos;
new_node->prve = pos->prve;
pos->prve->next= new_node;
pos->prve = new_node;
}
void list_erase(list_node* pos)
{
assert(pos);
pos->prve->next = pos->next;
pos->next->prve = pos->prve;
free(pos);
pos = NULL;
}
void list_destroy(list_node** pphead)
{
assert(pphead &&
*pphead);
list_node* pcur = (*pphead)->next;
while (pcur != *pphead)
{
list_node* del = pcur;
pcur = pcur->next;
free(del);
del = NULL;
}
free(*pphead);
*pphead = NULL;
pcur = NULL;
}
// test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
void list_test01()
{
list_node* plist;
list_init(&plist);
// 这里是传哨兵位的地址,因为plist的指向发生改变
list_push_back(plist, 1);
list_push_back(plist, 2);
list_push_back(plist, 3);
list_push_back(plist, 4);
list_push_back(plist, 5);
list_print(plist);
list_push_front(plist, 5);
list_push_front(plist, 4);
list_push_front(plist, 3);
list_push_front(plist, 2);
list_print(plist);
}
void list_test02()
{
list_node* plist;
list_init(&plist);
list_push_back(plist, 1);
list_push_back(plist, 2);
list_push_back(plist, 3);
list_push_back(plist, 4);
list_print(plist);
list_pop_back(plist);
list_print(plist);
list_pop_back(plist);
list_print(plist);
list_pop_back(plist);
list_print(plist);
list_pop_back(plist);
list_print(plist);
}
void list_test03()
{
list_node* plist;
list_init(&plist);
list_push_back(plist, 1);
list_push_back(plist, 2);
list_push_back(plist, 3);
list_push_back(plist, 4);
list_print(plist);
list_pop_front(plist);
list_print(plist);
list_pop_front(plist);
list_print(plist);
list_pop_front(plist);
list_print(plist);
}
void list_test04()
{
list_node* plist;
list_init(&plist);
list_push_back(plist, 1);
list_push_back(plist, 2);
list_push_back(plist, 3);
list_push_back(plist, 4);
list_print(plist);
list_node* pos = list_find(plist, 1);
list_insert(pos, 22);
list_print(plist);
pos = list_find(plist, 22);
list_erase(pos);
list_print(plist);
list_destroy(&plist);
}
int main()
{
//list_test01();
//list_test02();
//list_test03();
list_test04();
return 0;
}

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

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

相关文章

SpringBoot控制层接收参数处理、Logback日志入门和使用 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025-09-21 网站前几分钟还运行的好好地,几分钟后查看居然显示文件无法加载,访问首页提示无法访问此网站??!==ssl证书过期+域名解析失效

原来是用https访问的,ssl证书过期后就无法使用https访问了,文件所在的业务场景有可能需压强制使用https来访问,把ssl证书续上就行了, 还有就是查看你的服务器控制台,看看域名解析是否正常,我查看了一下,我去!域…

20231321王曦轶《密码系统设计》第二周

第一周预习报告 学习内容HeadFirstC嗨翻C语言第8章 《Windows C/C++加密解密实战》第 3,5 章第三章重点3.4,3.5,特别3.4.4 第五章重点5.3.7AI 对学习内容的总结(1分) 要求让AI(kimi,元宝等)阅读学习内容并进行总…

爱锋拍照工具 - 隐私政策

隐私政策概述 本隐私政策说明了拍照工具应用(以下简称"我们"或"应用")如何收集、使用、存储和保护您的个人信息。 🔍 信息收集 相机和媒体访问 相机权限: 用于扫描二维码和拍摄照片/视频 照片…

周计划+总结

这是第一期的周计划+总结(被我爸催的。。。。。后面一周一更 周一到周四我爸出差,周五有课,所以只有在下午能有时间做信息学,周一,周二,周五,每天做题,从题目池里面抽,周三周四用于补题和总结,并找相关的题目…

C#通讯之网络通讯 TCP UDP - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

[POI 2004] MOS

一道很有意思的贪心题,似乎noi导刊上有?记不太清了,反正是做出来了。 题意 有一个桥,一个火把,一堆人。 这对人要过桥,过桥有一些条件。需要过桥的人有火把不可同时过两个以上每次过桥的花费时间是两人中花费最高…

第03周 面向对象入门2与类的识别

什么样的方法应该用static修饰? static修饰的方法属于类本身,而不是类的实例。通常适用于以下几种情况: 比如Java中的Math类,用于创建对象的静态方法。 不用static修饰的方法往往具有什么特? 非static修饰的方法,…

完整教程:启用GPU对模型进行推理,安装cuda toolkit cuDNN 9

完整教程:启用GPU对模型进行推理,安装cuda toolkit &cuDNN 9pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "…

25秋周总结3

总结 这周的考试我还是挺稳的,并没有挂分,除了周一本来有 300pts 但是没有调出来比较可惜,其他场都已经打到上限了。所以现在我需要突破,如何做出更多的题?首先我要找出我不擅长的题,我发现最主要的就是一些需要…

R ggplot2学习Nature子刊一张图,换数据即可用! - 指南

R ggplot2学习Nature子刊一张图,换数据即可用! - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas&…

AI 在教育领域的落地困境:个性化教学与资料隐私的平衡之道

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025-06-10.购买联想thinkpad 16p

2025-06-10.购买联想thinkpad 16p 创建时间:2025-06-10 18:03 星期二 你的每一份记录都是美好的回忆,加油! 日记信息 地点:南京 心情:好 事件:购买联想笔记本电脑今日小记 事件1:2025-06-10.淘宝购买联想thinkp…

浏览器兼容性问题全解:CSS 前缀、Grid/Flex 布局兼容专业的方案与跨浏览器调试技巧

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

深入解析:大数据领域数据产品的深度学习应用

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

MySQL的Schema是什么? - 公众号

本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!🚀 魔都架构师 | 全网30W技术追随者 🔧 大厂分布式系统/数据中台实战专家 🏆 主导交易系统百万级流量调优 & 车联网平台架构 🧠 AIGC应用…

用户态与内核态的深度解析:安全、效率与优化之道 - 教程

用户态与内核态的深度解析:安全、效率与优化之道 - 教程2025-09-21 19:12 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important;…

推动安全研究多元化的10万美元捐赠计划

Trail of Bits宣布通过SummerCon会议捐赠10万美元支持年轻安全研究者,特别关注女性和少数群体。该计划包含资金资助、导师指导及会议演讲机会,并推动安全会议演讲者构成的多元化改革。Trail of Bits捐赠10万美元通过…

20250919

20250919T1 蒜头看演出 异或哈希即可。代码 #include <iostream> #include <string.h> #include <random> #include <set> #include <map> using namespace std; random_device rd; mt…