数据结构之双向带头循环链表

本文介绍双向带头循环链表的基本概念及其实现方法,包括链表的初始化、节点的插入与删除等常见操作。

一、链表的概念

链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。

双向带头循环链表的结构

在这里插入图片描述

二、链表的实现

杂项

1.结构定义

typedef int LDataType;
typedef struct ListNode
{
	struct ListNode* next;
	struct ListNode* prev;
	int data;
}LT;

2.链表打印

void Print(LT* phead);

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

3.申请空间

LT* BuyNode(int x);

LT* BuyNode(int x)
{
	LT* newnode = (LT*)malloc(sizeof(LT));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}
1.链表初始化
LT* ListIint();

LT* ListIint()
{
	LT* guard = (LT*)malloc(sizeof(LT));
	if (guard == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	guard->next = guard;
	guard->prev = guard;
	return guard;
}
2.尾插

在这里插入图片描述

void ListPushBack(LT* phead, int x);

void ListPushBack(LT* phead, int x)
{
	assert(phead);
	LT* node = BuyNode(x);

	LT* tail = phead->prev;
	tail->next = node;
	node->prev = tail;
	node->next = phead;
	phead->prev = node;
}
3.头插

在这里插入图片描述

void ListPushFront(LT* phead, int x);

void ListPushFront(LT* phead, int x)
{
	assert(phead);
	LT* next = phead->next;
	LT* node = BuyNode(x);

	node->next = next;
	node->prev = phead;
	phead->next = node;
	next->prev = node;
}
4.尾删

在这里插入图片描述

void ListPopBack(LT* phead);

void ListPopBack(LT* phead)
{
	assert(phead);
	assert(!ListEmpty(phead));
	LT* tail = phead->prev;
	phead->prev = tail->prev;
	tail->prev->next = phead;
	free(tail);
	tail = NULL;
}
5.头删

在这里插入图片描述

void ListPopFront(LT* phead);

void ListPopFront(LT* phead)
{
	assert(phead);
	assert(!ListEmpty(phead));
	LT* next = phead->next;
	phead->next = next->next;
	next->next->prev = phead;
	free(next);
	next = NULL;
}
6.链表长度
size_t ListSize(LT* phead);

size_t ListSize(LT* phead)
{
	assert(phead);
	int n = 0;
	LT* cur = phead->next;
	while (cur != phead)//遍历即可
	{
		n++;
		cur = cur->next;
	}
	return n;
}
7.数据查找
LT* ListFind(LT* phead, LDataType x);

LT* ListFind(LT* phead, LDataType x)
{
	assert(phead);
	LT* cur = phead->next;
	while (cur != phead)//遍历即可
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}
8.pos位置插入

在这里插入图片描述

void ListInsert(LT* pos, LDataType x);

void ListInsert(LT* pos, LDataType x)
{
	assert(pos);
	LT* prev = pos->prev;
	LT* newnode = BuyNode(x);

	prev->next = newnode;
	newnode->prev = prev;
	newnode->next = pos;
	pos->prev = newnode;
}
9.pos位置删除

在这里插入图片描述

void ListErase(LT* pos);

void ListErase(LT* pos)
{
	assert(pos);
	LT* prev = pos->prev;

	prev->next = pos->next;
	pos->next->prev = prev;
	free(pos);
	pos = NULL;
}
10.判断链表是否为空
bool ListEmpty(LT* phead);

bool ListEmpty(LT* phead)
{
	assert(phead);
	return phead->next == phead;
}

11.销毁
void ListDestory(LT* phead);

void ListDestory(LT* phead)
{
	assert(phead);
	LT* cur = phead->next;
	while (cur != phead)
	{
		LT* next = cur->next;
		free(cur);
		cur = next;
	}

	free(phead);
	
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值