数据结构3——双向链表

news2024/9/25 4:47:10

在上篇文章数据结构2——单链表中,我们了解了什么是链表,以及单链表的实现,接着上篇文章,本篇文章介绍一下比单链表更常用的链表——双向循环链表。


目录

1.双向链表的实现

1.1节点

1.2 初始化节点

1.3 动态开辟新节点

1.4 遍历链表

1.5 查找

1.6 插入

1.尾插

 2. 头插

3.在pos之前插入 

1.7 删除

1. 尾删

 2. 头删

 3.删除pos位置

1.8 其他函数

1. 链表长度

2. 销毁链表

1.9 测试

2.顺序表和链表的比较

*源代码

DLish.h

DLish.c

test.c


 

1.双向链表的实现

1.1节点

单链表的节点存的有数据域和一个指向下一个节点后继指针,因此单链表只能单向遍历。

要想实现双向遍历,就需要在节点里增加一个指向前一个节点的前驱指针。

typedef int DLTDataType;
typedef struct DListNode
{
	DLTDataType data;
	struct DListNode* prev;//前继指针
	struct DListNode* next;//后继指针
}DLTNode;

1.2 初始化节点

//初始化节点函数实现
DLTNode* DLTInit()
{
	DLTNode* phead = BuyDLTNode(-1);
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

1.3 动态开辟新节点

//开辟新节点函数实现
DLTNode* BuyDLTNode(DLTDataType x)
{
	DLTNode* node = (DLTNode*)malloc(sizeof(DLTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	node->data = x;
	node->next = NULL;
	node->prev = NULL;

	return node;
}

1.4 遍历链表

void DLTPrint(DLTNode* phead)
{
	assert(phead);

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

1.5 查找

//查找
DLTNode* DLTFind(DLTNode* phead, DLTDataType x)
{
	assert(phead);

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}

	return NULL;
}

1.6 插入

1.尾插

//尾插函数实现
void DLTPushBack(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	//①常规写法
	DLTNode* tail = phead->prev;
	DLTNode* newnode = BuyDLTNode(x);

	newnode->prev = tail;
	tail->next = newnode;

	newnode->next = phead;
	phead->prev = newnode;
	//②复用 DLTInsert
	//DLTInsert(phead, x);
}

 2. 头插

//头插函数实现
void DLTPushFront(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	//①常规写法
	DLTNode* newnode = BuyDLTNode(x);

	newnode->next = phead->next;
	phead->next->prev = newnode;

	phead->next = newnode;
	newnode->prev = phead;
	//②复用DLTInsert
	//DLTInsert(phead->next, x);
}

3.在pos之前插入 

//在pos之前插入
void DLTInsert(DLTNode* pos, DLTDataType x)
{
	assert(pos);

	DLTNode* posPrev = pos->prev;
	DLTNode* newnode = BuyDLTNode(x);

	posPrev->next = newnode;
	newnode->prev = posPrev;
	newnode->next = pos;
	pos->prev = newnode; 
}

1.7 删除

1. 尾删

//尾删函数实现
void DLTPopBack(DLTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);//检查链表是否为空

	//①常规写法
	DLTNode* tail = phead->prev;
	DLTNode* tailPrev = tail->prev;
	free(tail);

	tailPrev->next = phead;
	phead->prev = tailPrev;
	//②复用DLTErase
	//DLTErase(phead->prev);
}

 2. 头删

//头删函数实现
void DLTPopFront(DLTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	//①常规写法
	DLTNode* first = phead->next;
	DLTNode* second = first->next;

	free(first);

	phead->next = second;
	second->prev = phead;
	//②复用DLTErase
	//DLTErase(phead->next);
}

 3.删除pos位置

//删除pos位置
void DLTErase(DLTNode* pos)
{
	assert(pos);
	DLTNode* posPrev = pos->prev;
	DLTNode* posNext = pos->next;

	free(pos);

	posPrev->next = posNext;
	posNext->prev = posPrev;
}

1.8 其他函数

1. 链表长度

//链表长度
int DLTSize(DLTNode* phead)
{
	assert(phead);

	int size = 0;
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		++size;
		cur = cur->next;
	}

	return size;
}

2. 销毁链表

//销毁函数
void DLTDestory(DLTNode* phead)
{
	assert(phead);

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		DLTNode* next = cur->next;
		free(cur);

		cur = next;
	}

	free(phead);

}

1.9 测试

int main()
{
	DLTNode* plist = DLTInit();
	DLTPushBack(plist, 1);
	DLTPushBack(plist, 2);
	DLTPushBack(plist, 3);
	DLTPushBack(plist, 4);
	DLTPushBack(plist, 5);
	DLTPrint(plist);
	DLTPushFront(plist, 10);
	DLTPushFront(plist, 11);
	DLTPushFront(plist, 12);
	DLTPrint(plist);
	DLTDestory(plist);
	plist = NULL;
	return 0;
}

2.顺序表和链表的比较

不同点顺序表链表
存储空间上物理上一定连续逻辑上连续,但物理上不一定 连续
随机访问支持O(1)不支持O(N)
任意位置插入或者删除元素可能需要搬移元素,效率低 O(N)只需修改指针指向
插入动态顺序表,空间不够时需要 扩容没有容量的概念
应用场景元素高效存储+频繁访问任意位置插入和删除频繁
缓存利用率


*源代码

DLish.h

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int DLTDataType;
typedef struct DListNode
{
	DLTDataType data;
	struct DListNode* prev;//前继指针
	struct DListNode* next;//后继指针
}DLTNode;

//开辟新节点函数声明
DLTNode* BuyDLTNode(DLTDataType x);

//初始化节点函数声明
DLTNode* DLTInit();

//遍历函数声明
void DLTPrint(DLTNode* phead);

//尾插函数声明
void DLTPushBack(DLTNode* phead, DLTDataType x);
//头插函数声明
void DLTPushFront(DLTNode* phead, DLTDataType x);

//尾删函数声明
void DLTPopBack(DLTNode* phead);
//头删函数声明
void DLTPopFront(DLTNode* phead);

//链表长度
int DLTSize(DLTNode* phead);

//查找
DLTNode* DLTFind(DLTNode* phead, DLTDataType x);

//在pos之前插入 
void DLTInsert(DLTNode* pos, DLTDataType x);
//删除pos位置
void DLTErase(DLTNode* pos);

//销毁函数
void DLTDestory(DLTNode* phead);

DLish.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "DList.h"

//开辟新节点函数实现
DLTNode* BuyDLTNode(DLTDataType x)
{
	DLTNode* node = (DLTNode*)malloc(sizeof(DLTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	node->data = x;
	node->next = NULL;
	node->prev = NULL;

	return node;
}

//初始化节点函数实现
DLTNode* DLTInit()
{
	DLTNode* phead = BuyDLTNode(-1);
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

//遍历函数实现
void DLTPrint(DLTNode* phead)
{
	assert(phead);

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


//尾插函数实现
void DLTPushBack(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	//①常规写法
	DLTNode* tail = phead->prev;
	DLTNode* newnode = BuyDLTNode(x);

	newnode->prev = tail;
	tail->next = newnode;

	newnode->next = phead;
	phead->prev = newnode;
	//②复用 DLTInsert
	//DLTInsert(phead, x);
}
//头插函数实现
void DLTPushFront(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	//①常规写法
	DLTNode* newnode = BuyDLTNode(x);

	newnode->next = phead->next;
	phead->next->prev = newnode;

	phead->next = newnode;
	newnode->prev = phead;
	//②复用DLTInsert
	//DLTInsert(phead->next, x);
}



//尾删函数实现
void DLTPopBack(DLTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);//检查链表是否为空

	//①常规写法
	DLTNode* tail = phead->prev;
	DLTNode* tailPrev = tail->prev;
	free(tail);

	tailPrev->next = phead;
	phead->prev = tailPrev;
	//②复用DLTErase
	//DLTErase(phead->prev);
}
//头删函数实现
void DLTPopFront(DLTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	//①常规写法
	DLTNode* first = phead->next;
	DLTNode* second = first->next;

	free(first);

	phead->next = second;
	second->prev = phead;
	//②复用DLTErase
	//DLTErase(phead->next);
}

//链表长度
int DLTSize(DLTNode* phead)
{
	assert(phead);

	int size = 0;
	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		++size;
		cur = cur->next;
	}

	return size;
}

//查找
DLTNode* DLTFind(DLTNode* phead, DLTDataType x)
{
	assert(phead);

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}

	return NULL;
}

//在pos之前插入
void DLTInsert(DLTNode* pos, DLTDataType x)
{
	assert(pos);

	DLTNode* posPrev = pos->prev;
	DLTNode* newnode = BuyDLTNode(x);

	posPrev->next = newnode;
	newnode->prev = posPrev;
	newnode->next = pos;
	pos->prev = newnode; 
}
//删除pos位置
void DLTErase(DLTNode* pos)
{
	assert(pos);
	DLTNode* posPrev = pos->prev;
	DLTNode* posNext = pos->next;

	free(pos);

	posPrev->next = posNext;
	posNext->prev = posPrev;
}

//销毁函数
void DLTDestory(DLTNode* phead)
{
	assert(phead);

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		DLTNode* next = cur->next;
		free(cur);

		cur = next;
	}

	free(phead);

}

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "DList.h"

int main()
{
	DLTNode* plist = DLTInit();
	DLTPushBack(plist, 1);
	DLTPushBack(plist, 2);
	DLTPushBack(plist, 3);
	DLTPushBack(plist, 4);
	DLTPushBack(plist, 5);
	DLTPrint(plist);
	DLTPushFront(plist, 10);
	DLTPushFront(plist, 11);
	DLTPushFront(plist, 12);
	DLTPrint(plist);
	DLTDestory(plist);
	plist = NULL;
	DLTPrint(plist);
	return 0;
}

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

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

相关文章

美团中间件C++一面-面经总结

1、TCP和UDP 的区别&#xff1f; 速记标识符&#xff1a;连靠刘墉宿营 解释&#xff1a; 面向连接vs无连接 可靠传输vs不保证可靠 字节流vs报文传输 拥塞控制流量控制vs无 速度慢vs速度快 应用场景自己描述 2、服务端处于close wait是什么情况&#xff0c;是由什么造成的&…

【算法】模拟:(leetcode)495.提莫攻击(easy)

目录 题目链接 题目介绍 解法 代码 题目链接 495. 提莫攻击 - 力扣&#xff08;LeetCode&#xff09; 题目介绍 解法 模拟 分情况讨论。 当寒冰再次中毒时&#xff0c;上次「中毒」是否已经结束。 ​ 当上次中毒已经结束了&#xff0c;那么上次「中毒」维持的时间就是 …

C++【类和对象】(构造函数与析构函数)

文章目录 1. 类的默认成员函数2. 构造函数析构函数的特点3. 析构函数析构函数的特点 结语 1. 类的默认成员函数 默认成员对象就是我们没有显示的写&#xff0c;但是编译器会自动生成的成员函数。一个类&#xff0c;我们不写的情况下编译器会默认生成以下6个成员函数&#xff0…

【JVM】一篇文章彻底理解JVM的组成,各组件的底层实现逻辑

文章目录 JVM 的主要组成部分类加载器&#xff08;Class Loader&#xff09;1. 加载&#xff08;Loading&#xff09;2. 链接&#xff08;Linking&#xff09;3. 初始化&#xff08;Initialization&#xff09; Execution Engine&#xff08;执行引擎&#xff09;1. 解释器&…

微服务--Docker

Docker是一个开源的应用容器引擎&#xff0c;它基于Go语言并遵从Apache2.0协议开源。Docker提供了一种轻量级、可移植和自包含的容器化环境&#xff0c;使开发人员能够在不同的计算机上以一致的方式构建、打包和分发应用程序。 一、Docker的基本概念 容器&#xff08;Contain…

828华为云征文|使用Flexus X实例创建FDS+Nginx服务实现图片上传功能

一、Flexus X实例 什么是Flexus X实例呢&#xff0c;这是华为云最新推出的云服务器产品&#xff0c;如下图&#xff1a; 华为云推出的Flexus云服务器X系列&#xff0c;是在华为顶尖技术团队&#xff0c;特别是荣获国家科技进步奖的领军人物顾炯炯博士及其团队的主导下精心研发…

AWS注册时常见错误处理

引言 创建AWS账号是使用AWS云服务的第一步&#xff0c;但在注册过程中可能会遇到一些常见的问题。本文中九河云将帮助您排查和解决在创建AWS账户时可能遇到的一些常见问题&#xff0c;包括未接到验证电话、最大失败尝试次数错误以及账户激活延迟等。 常见问题及解决方法 1. …

记一次Mac 匪夷所思终端常用网络命令恢复记录

一天莫名奇妙发现ping dig 等基础命令都无法正常使用。还好能浏览器能正常访问&#xff0c;&#xff0c;&#xff0c;&#xff0c; 赶紧拿baidu试试^-^ ; <<>> DiG 9.10.6 <<>> baidu.com ;; global options: cmd ;; connection timed out; no serve…

leetcode24. 两两交换链表中的节点,递归

leetcode24. 两两交换链表中的节点 给你一个链表&#xff0c;两两交换其中相邻的节点&#xff0c;并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题&#xff08;即&#xff0c;只能进行节点交换&#xff09;。 示例 1&#xff1a; 输入&#xff1a;he…

[译] K8s和云原生

本篇内容是根据2019年8月份Kubernetes and Cloud Native音频录制内容的整理与翻译, Johnny 和 Mat 与 Kris Nova 和 Joe Beda 一起探讨了 Kubernetes 和云原生。他们讨论了 Kubernetes 推动的“云原生”应用的兴起、使用 Kubernetes 的合适场合、运行如此大型的开源项目所面临…

【Java】虚拟机(JVM)内存模型全解析

目录 一、运行时数据区域划分 版本的差异&#xff1a; 二、程序计数器 程序计数器主要作用 三、Java虚拟机 1. 虚拟机运行原理 2. 活动栈被弹出的方式 3. 虚拟机栈可能产生的错误 4. 虚拟机栈的大小 四、本地方法栈 五、堆 1. 堆区的组成&#xff1a;新生代老生代 …

叶国富学得会胖东来吗?

“大家都看不懂就对了&#xff0c;如果都看得懂我就没有机会了。”昨晚&#xff0c;实体零售迎来一则重磅消息&#xff0c;名创优品获得了全国第二大连锁超市永辉超市的大股东身份。在资本市场负反馈的压力下&#xff0c;名创优品创始人叶国富有了上述回应。 消息公布后&#x…

云服务器是干什么的?

随着云计算的发展&#xff0c;云服务器的功能逐步完善。但是还有不少用户不清楚云服务器是干什么的&#xff1f;云服务器提供了一种灵活、可扩展的计算解决方案&#xff0c;适用于各种在线业务和项目。提供虚拟化的计算资源是云服务器最基本也是最重要的功能。 云服务器是干什…

vue项目npm run serve 报错,Error: read ECONNRESET at TCP.onStreamRead

背景&#xff1a;vue2的项目&#xff0c;之前npm run serve一直可以正常使用&#xff0c;突然每次启动都会报错了&#xff0c;报错信息如下&#xff1a; node:events:492 throw er; // Unhandled error event ^ Error: read ECONNRESET at TCP.onStreamRead (n…

Android开发okhttp下载图片带进度

Android开发okhttp下载图片带进度 下载网络图片的方法有很多&#xff0c;这次介绍写用okhttp来下载网络图片&#xff0c;主要我看中的是用okhttp下载有进度返回&#xff0c;提示下用户 一、思路&#xff1a; 用OkHttpClient().newCall(request) 二、效果图&#xff1a; 三、…

“加密自由”受到威胁?Telegram已被“收编”?成立审核团队,提供用户数据!

近年来&#xff0c;随着加密货币和区块链技术的迅猛发展&#xff0c;Telegram作为一个重要的社交平台&#xff0c;因其对用户隐私的承诺和自由交流的环境&#xff0c;吸引了大量的用户。目前&#xff0c;Telegram已经成为加密货币市场最爱使用的全球通讯软件。 然而&#xff0c…

成都睿明智科技有限公司抖音开店怎么样?

在当今这个短视频与直播带货风靡的时代&#xff0c;抖音电商以其独特的魅力迅速崛起&#xff0c;成为众多品牌和企业竞相追逐的新蓝海。而在这场电商盛宴中&#xff0c;成都睿明智科技有限公司凭借其专业的服务、创新的策略以及深厚的行业洞察力&#xff0c;成为了众多商家信赖…

828华为云征文 | 云服务器Flexus X实例,Docker集成搭建斗地主

828华为云征文 | 云服务器Flexus X实例&#xff0c;Docker集成搭建斗地主 华为云端口放行 服务器放行对应端口8078 Docker安装并配置镜像加速 1、购买华为云 Flexus X 实例 Flexus云服务器X实例-华为云 (huaweicloud.com) 2、docker安装 yum install -y docker-ce3、验证 Dock…

第52课 Scratch游戏入门:五子棋

五子棋 故事背景: 会下五子棋么?五个颜色一样的棋子,横竖斜向有五个连在一起,就胜利,让我们一起来绘制一个五子棋的棋盘,同时一起开始下棋吧! 开始编程 1、删除预设的猫咪角色,使用绘制工具绘制白色和黑色的棋子。(使用圆形和圆形渐变色填充棋子) 新绘制棋盘等其他角…

刷题日记_DAY1

前言 这里记录每日随机刷的错题 两个数组的交集&#xff08;模拟&#xff09; 题目描述 题目解析 题目要求返回指定的两个字符串之间的距离&#xff0c;容易想到的一种解法就是暴力遍历&#xff0c;来个双循环&#xff0c;但时间复杂度就为N^2&#xff0c;不符合题意 for(…