双链表及其功能实现
创始人
2025-05-30 13:16:59

实现功能

ListBuyNode ————————————申请一个新节点并赋值

ListInit ———————————————计算链表的长度

ListDestory —————————————销毁链表

ListPushBack ————————————尾插

ListPushFront ————————————头插

ListPopBack —————————————尾删

ListPopFront ————————————头删

ListFindByVal ————————————按值查找链表

ListInsertAfter ————————————任意位置插入

ListErase ——————————————任意位置删除

ListPrint ——————————————打印链表

List.h

#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include
#include
#include
#includetypedef int ListDataType;typedef struct ListNode
{struct ListNode* preview;struct ListNode* next;ListDataType val;
}ListNode;extern ListNode* ListBuyNode(ListDataType x);//创建一个节点
extern ListNode* ListInit();//链表初始化,创造表头
extern void DListDestory(ListNode** pphead);//销毁
extern void ListPrint(ListNode* phead);//打印
extern void ListPushBack(ListNode* phead, ListDataType x);//尾插
extern void ListPopBack(ListNode* phead);//尾删
extern void ListPushFront(ListNode* phead, ListDataType x);//头插
extern void ListPopFront(ListNode* phead);//头删
extern ListNode* ListFindVal(ListNode* phead, ListDataType x);//按值查找
extern void ListInsertAfter(ListNode* pos, ListDataType x);//在任意位置插入
extern void ListErase(ListNode* pos);//在任意位置删除

List.c

#include "DList.h"ListNode* ListInit()
{ListNode* phead = ListBuyNode(0);phead->preview = phead;phead->next = phead;return phead;
}void DListDestory(ListNode** pphead)
{ListNode* cur = (*pphead)->next;while (cur != (*pphead)){cur = cur->next;free(cur->preview);}free(cur);*pphead = NULL;
}ListNode* ListBuyNode(ListDataType x)
{ListNode* newnode = malloc(sizeof(ListNode));assert(newnode);newnode->val = x;newnode->preview = NULL;newnode->next = NULL;return newnode;
}void ListPushBack(ListNode* phead, ListDataType x)
{assert(phead);ListNode* newnode = ListBuyNode(x);assert(newnode);phead->preview->next = newnode;newnode->preview = phead->preview;newnode->next = phead;phead->preview = newnode;
}bool ListEmpty(ListNode* phead)
{return (phead->next == phead) ? true : false;
}
void ListPrint(ListNode* phead)
{assert(phead);if (ListEmpty(phead))printf("List is empty!");for (ListNode* cur = phead->next; cur != phead; cur = cur->next)printf("%d ", cur->val);printf("\n");
}void ListPopBack(ListNode* phead)
{assert(phead && phead->next != phead);ListNode* tail = phead->preview;phead->preview = tail->preview;tail->preview->next = phead;free(tail);
}void ListPushFront(ListNode* phead, ListDataType x)
{assert(phead);ListNode* newnode = ListBuyNode(x);phead->next->preview = newnode;newnode->next = phead->next;phead->next = newnode;newnode->preview = phead;
}void ListPopFront(ListNode* phead)
{assert(phead && phead->next != phead);ListNode* cur = phead->next;phead->next = cur->next;cur->next->preview = phead;free(cur);
}ListNode* ListFindVal(ListNode* phead, ListDataType x)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){if (cur->val == x)return cur;cur = cur->next;}return NULL;
}void ListInsertAfter(ListNode* pos, ListDataType x)
{assert(pos);ListNode* newnode = ListBuyNode(x);newnode->preview = pos;newnode->next = pos->next;pos->next->preview = newnode;pos->next = newnode;
}void ListErase(ListNode* pos)
{assert(pos && pos->next != pos);ListNode* prev = pos->preview;ListNode* next = pos->next;prev->next = next;next->preview = prev;free(pos);
}

test.c

#include "DList.h"void test1()
{ListNode* pList = ListInit();ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);DListDestory(&pList);
}void test2()
{ListNode* pList = ListInit();ListPushBack(pList, 5);ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPrint(pList);
}void test3()
{ListNode* pList = ListInit();ListPushBack(pList, 5);ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);ListPopFront(pList);ListPopFront(pList);ListPopFront(pList);ListPopFront(pList);ListPrint(pList);ListPopFront(pList);ListPrint(pList);
}void test4()
{ListNode* pList = ListInit();ListInsertAfter(pList, 5);ListInsertAfter(pList, 4);ListInsertAfter(pList, 1);ListInsertAfter(pList->next, 3);ListInsertAfter(pList->next, 2);ListPrint(pList);ListErase(pList->next->next->next->next);ListNode* cur = ListFindVal(pList, 4);if (cur != NULL)ListErase(cur);elseprintf("The node with this value does not exist!\n");ListPrint(pList);ListErase(pList->next);ListErase(pList->next);ListErase(pList->next);ListPrint(pList);
}void main()
{printf("---------------------test1----------------------\n");test1();printf("---------------------test2----------------------\n");test2();printf("---------------------test3----------------------\n");test3();printf("---------------------test4----------------------\n");test4();
}

相关内容

热门资讯

数字操作方法 系列文章目录 前端系列文章——传送门 JavaScript系列文章——传送门 文章目录系列文章目录...
Cartesi 2023 年 ... 查看 Cartesi Machine、Cartesi Rollups 和 Noether 的更新正在...
JavaWeb——jsp概述入... JSP定义:  在如下一个jsp文件里面有如下的代码  <%@ page content...
一切喜怒哀乐都来自于你的认知 01 有个学子,准备出国,父母请来清华的教授宁向东。请问教授࿱...
JAVA并发编程——synch... 引言         Java语言为了解决并发编程中存在的原子性、可见性和有序性问题,...
git学习----3.21 未... 文章目录前言Git :一个分布式版本控制工具目标一、概述1.1 开发中的实际场景1.2...
Qt优秀开源项目之十七:QtP... QtPromise是Promises/A+规范的Qt/C++实现。该规范的译...
【前端八股文】JavaScri... 文章目录Set概念与arr的比较属性和方法并集、交集、差集Map概念属性和方法String用索引值和...
海康硬盘录像机接入RTSP/o... EasyNVR安防视频云服务平台可支持设备通过RTSP/Onvif协议接入平台,能提供...
在混合劳动力时代如何避免网络安... 在混合劳动力时代如何避免安全网络风险 三年多来,混合工作一直是工作生活中不可或缺的一...
2023还不懂Jmeter接口... 这里介绍的Jmeter接口测试的的实战,如果文章内容没遇看懂的话,我这边...
基于4G/5G弱网聚合的多链路... 基于4G/5G多卡聚合(弱网聚合)的智能融合通信设备技术亮点 增强带宽提供可靠连接 通过将多个有线和...
如何使用Synplify综合v... 文章目录使用Synplify综合的好处synplify的教程方法1(无效)...
2023年全国最新高校辅导员精... 百分百题库提供高校辅导员考试试题、辅导员考试预测题、高校辅导员考试真题、辅导员证考试题库等ÿ...
2022年18个值得期待的Le... 有数百个独特的LearnDash附加组件,您可能很难选择您的LearnDash LMS...
【java基础】Stream流... 文章目录基本介绍流的创建流的各种常见操作forEach方法filter方法map方法peek方法fl...
javaweb高校行政办公自动... 本课题基于我国高校管理信息化建设现状,结合在实际工作中所遇到的问题和收获,...
一款专门为自动化测试打造的集成... 你好,我是不二。 随着行业内卷越来越严重,自动化测试已成为测试工程师的...
【go-zero】golang... 一、casbin 概览 1、casbin基本了解 casbin的GitHub:https://git...
现在开发低代码平台算晚吗? 现在开发低代码平台算晚吗?作为低代码的亲戚——零代码厂商,这篇就以“厂商...
【JavaWeb】书城项目(2... 222.书城项目-第三阶段:修改所有html页面为jsp页面 改成jsp页面之后&#x...
基于jeecgboot的大屏设...      通过前面设计好数据源后,就要进行数据集的设计了。      一、还是在onl...
Linux命令小技巧:显示文件... 工作中会有很多千奇百怪的需求,比如:如何在 Linux 命令行中快速找到...
【找工作】-- 大数据工程师找... 目录 1.前言 2.找工作的理论知识 2.1 分析个人特征 2.1.1 你自身优势是什么?
C++基础算法④——排序算法(... 排序算法 1.插入排序 2.桶排序 1.插入排序 基本思想:将初始数据分为有序部分和...
nginx快速入门.跟学B站n... nginx快速入门.跟学B站nginx一小时精讲课程笔记nginx简介及环境准备nginx简介环境准...
ORACLE存过互相调用之间事... 今天在问答区看到一个问题是 假如有procedureA、procedureB和procedureC&...
基于java中Springbo... 基于java中Springboot框影视影院订票选座管理系统 开发语言:Java 框...
CVE-2018-18086 最近闲来无事,看到青少年CTF平台,感觉对新手还是比较友好的࿰...
【深度学习】基于Hough变化... 💥💥💞💞欢迎来到本博客❤️❤️&#x...