数据结构:双向链表

文章目录

  • [1. 双向带头循环链表的结构](#1. 双向带头循环链表的结构)
  • [2. 相关操作](#2. 相关操作)
    • [2.1 创建节点](#2.1 创建节点)
    • [2.2 尾插](#2.2 尾插)
    • [2.3 头插](#2.3 头插)
    • [2.4 打印](#2.4 打印)
    • [2.5 尾删](#2.5 尾删)
    • [2.6 头删](#2.6 头删)
    • [2.7 查找](#2.7 查找)
    • [2.8 指定位置前/后插入](#2.8 指定位置前/后插入)
    • [2.9 删除指定位置的节点](#2.9 删除指定位置的节点)
    • [2.10 删除指定位置后的节点](#2.10 删除指定位置后的节点)
    • [2.11 销毁链表](#2.11 销毁链表)
  • 3.顺序表与链表区别

1. 双向带头循环链表的结构

与单链表不同的是:

  • 双向链表有一个"哨兵位"作为单独的头节点
  • 每个节点都可以指向其前驱和后继节点
  • 链表是循环的

带头链表里的头节点,实际为"哨兵位",哨兵位节点不存储任何有效元素,只是站在这里"放哨的"
"哨兵位"存在的意义:遍历循环链表避免死循环。

c 复制代码
typedef int ListDataType;
typedef struct List
{
	ListDataType data;
	struct List* prev;//指向前驱节点
	struct List* next;//指向后继节点
}List;

2. 相关操作

2.1 创建节点

  • 创建的每个节点应该自己成环
c 复制代码
List* BuyNode(ListDataType x)
{
	List* newnode = (List*)malloc(sizeof(List));
	if (newnode == NULL)
	{
		perror(malloc);
		return NULL;
	}
	newnode->data = x;
	newnode->next = newnode;
	newnode->prev = newnode;
}

创建哨兵位:

c 复制代码
int main()
{
	//哨兵位
	List* head = BuyNode(-1);
	return 0;
}

2.2 尾插

c 复制代码
void ListPushBack(List* phead, ListDataType x)
{
	assert(phead);
	List* newnode = BuyNode(x);

	newnode->next = phead;
	newnode->prev = phead->prev;
	//尾节点指向新节点
	phead->prev->next = newnode;
	
	phead->prev = newnode;
}

2.3 头插

c 复制代码
void ListPushFront(List* phead, ListDataType x)
{
	assert(phead);
	List* newnode = BuyNode(x);

	newnode->next = phead->next;
	newnode->prev = phead;
	phead->next = newnode;
	//只有头节点时,就是头节点的prev
	phead->next->prev = newnode;
}

2.4 打印

由于是循环链表,所以循环停止的条件应该是:cur != head

c 复制代码
void ListPrint(List* phead)
{
	assert(phead);

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

2.5 尾删

  • 不能没有节点
  • 尾节点的前驱节点指向头节点
  • 头节点指向尾节点的前驱节点
  • 释放尾节点
c 复制代码
void ListPopBack(List* phead)
{
	assert(phead);
	//只有头节点
	assert(phead->next != phead);

	List* del = phead->prev;
	del->prev->next = phead;
	phead->prev = del->prev;
	free(del);
}

2.6 头删

  • 不能没有节点
  • 待删除节点的后继节点的prev指向头节点
  • 头节点指向待删除节点的后继节点
c 复制代码
void ListPopFront(List* phead)
{
	assert(phead);
	assert(phead->next != phead);

	List* del = phead->next;
	del->next->prev = phead;
	phead->next = del->next;
	free(del);
}

2.7 查找

c 复制代码
List* ListFind(List* phead, ListDataType x)
{
	assert(phead);
	assert(phead->next != phead);
	List* cur = phead->next;

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

2.8 指定位置前/后插入

  • 将新节点与指定位置相连即可

指定位置前

c 复制代码
void ListPosFrontInsert(List* pos, ListDataType x)
{
	assert(pos);

	List* newnode = BuyNode(x);
	List* prev = pos->prev;

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

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

指定位置后

c 复制代码
void ListPosBackInsert(List* pos, ListDataType x)
{
	assert(pos);

	List* newnode = BuyNode(x);
	List* next = pos->next;

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

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

2.9 删除指定位置的节点

  • 将指定位置的前驱节点、后继节点连接即可
c 复制代码
void ListPosDel(List* pos)
{
	assert(pos);

	List* prev = pos->prev;
	List* next = pos->next;

	prev->next = next;
	next->prev = prev;
	free(pos);
	pos = NULL;
}

2.10 删除指定位置后的节点

  • 若指定位置是尾节点,则应该执行头删
c 复制代码
void ListPosAfterDel(List* phead, List* pos)
{
	assert(phead);
	assert(pos);
	if (pos->next == phead)
	{
		ListPopFront(phead);
	}
	else
	{
		List* del = pos->next;
		del->next->prev = pos;
		pos->next = del->next;
		free(del);
	}
}

2.11 销毁链表

  • 此接口的参数是一级指针,所以并不能将哨兵位销毁;因此需要再调用处再将哨兵位置为空
c 复制代码
void ListDestroy(List* phead)
{
	assert(phead);
	assert(phead->next != phead);

	List* cur = phead->next;
	while (cur != phead)
	{
		List* next = cur->next;
		free(cur);
		cur = next;
	}
	cur = NULL;
	free(phead);
	phead = NULL;
}

3.顺序表与链表区别

不同点 顺序表 链表
存储空间上 物理上一定连续 逻辑上连续,物理上不一定连续
随机访问 O(1) O(N)
任意位置插入或删除 可能需要搬运元素,效率低O(N) 只需修改指针指向
插入 动态顺序表,空间不够需要扩容 没有容量的概念
应用场景 元素高效存储,频繁访问 任意位置频繁插入/删除
缓存利用率
相关推荐
血小板要健康11 分钟前
189.轮转数组,力扣
数据结构·算法·leetcode
无限进步_41 分钟前
203. 移除链表元素 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
10岁的博客1 小时前
C语言造轮子大赛
java·c语言·数据结构
努力努力再努力wz1 小时前
【Linux网络系列】:打破 HTTP 明文诅咒,在Linux 下用 C++ 手搓 HTTPS 服务器全过程!(附实现源码)
linux·服务器·网络·数据结构·c++·http·https
@Aurora.1 小时前
优选算法【专题七:分治】
数据结构·算法·排序算法
沉默-_-1 小时前
力扣hot100普通数组(1)--C++
java·数据结构·算法·leetcode·数组
爱吃生蚝的于勒1 小时前
【Linux】进程信号的保存(二)
linux·运维·服务器·c语言·数据结构·c++·算法
im_AMBER1 小时前
Leetcode 108 交换链表中的节点
数据结构·笔记·学习·算法·leetcode·链表
啊阿狸不会拉杆2 小时前
《数字信号处理》第 4 章-快速傅里叶变换 (FFT)
数据结构·人工智能·算法·机器学习·信号处理·数字信号处理·dsp
带鱼吃猫2 小时前
数据结构:栈与队列的核心概念与模拟实现
数据结构