(学习记录)双向带头循环链表


概述

双向带头循环链表,它首先必须有一个带哨兵位的头结点。哨兵位的prev和next都指向它自己
哨兵位
假设链表有三个值(1,2,3),如图,则

  1. 哨兵位的next指向1
    哨兵位的prev指向3
  2. 1的next指向2
    1的prev指向哨兵位
  3. 2的next指向3
    2的prev指向1
  4. 3的next指向哨兵位
    3的prev指向2
    图一

这就是一个完整的双向带头循环链表。


链表结构

首先,创建头文件(List.h), 函数实现文件(List.c)和测试文件(test.c)。

在List.h中:

  1. 将数据类型重命名,方便以后更改类型。
  2. 将结构体重命名,方便书写并简化代码。

如下图可知,结构体所需要的是指向前后的两个指针所存数据
在这里插入图片描述
那么,定义一个数据类型用来存放数据,一个prev的结构体指针和一个next的结构体指针

代码如下:

typedef int LTDataType;

typedef struct ListNode
{
	LTDataType data;
	struct ListNode* prev;
	struct ListNode* next;
}ListNode;

尾插

尾插时,所需要的的参数

  1. 链表的头结点
  2. 想要插入的数据

在List.h中声明尾插函数void ListPushBack(ListNode* phead, LTDataType x);

实现:

  1. 创建新的节点。

在List.h中声明函数ListNode* BuyListNode(LTDataType x);
在List.c中实现函数

ListNode* BuyListNode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}
  1. 需要断言。因为在带头链表中,最开始就拥有哨兵位
  2. 不需要找到尾结点。因为在双向循环链表中,尾结点的next指向哨兵位,哨兵位的prev指向尾结点。

如图所示,尾结点(tail)是头节点(phead)的prev。
在这里插入图片描述
插入newnode后,如图:

  1. tail->next 指向 newnode
  2. newnode->prev 指向tail
  3. newnode->next指向头节点(phead)
  4. phead->prev 指向newnode

在这里插入图片描述
在在List.c中实现函数

void ListPushBack(ListNode* phead, LTDataType x)
{
	assert(phead);

	ListNode* tail = phead->prev;
	ListNode* newnode = BuyListNode(x);

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

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

时间复杂度为O(1)。

尾插测试

在测试(test.c)中,创建一个头节点(pList)ListNode* pList = NULL;
但这样pList是指向空,无法尾插,所以pList要改为如下图所示的结构。
由于改变的是结构体的指针,要传结构体指针的地址过去。所以要用二级指针。
在这里插入图片描述

List.h声明初始化函数void ListInit(ListNode** pphead);
List.c实现函数

void ListInit(ListNode** pphead)
{
	*pphead = BuyListNode(0);
	(*pphead)->next = *pphead;
	(*pphead)->prev = *pphead;
}

最后在test.c中监视窗口查看结果。

void TestLTNodePushBack()
{
	ListNode* pList = NULL;
	ListInit(&pList);
	ListPushBack(pList, 1);
	ListPushBack(pList, 2);
	ListPushBack(pList, 3);
	ListPushBack(pList, 4);
}

或者使用打印函数

  1. List.h中声明函数void ListPrint(ListNode* phead);
  2. 由于哨兵位不打印,所以从哨兵位下一个节点开始遍历,遍历到哨兵位结束

实现代码:

void ListPrint(ListNode* phead)
{
	assert(phead);
	ListNode* cur = phead->next;//哨兵位下一个节点
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

尾删

List.h中声明函数void ListPopBack(ListNode* phead);
在这里插入图片描述

如上下两图所示,想要尾删,就需要知道尾结点(tail)的前一个地址(tailPrev)。

  1. 将tailPrev->next指向哨兵位(phead)
  2. 将pheadprev指向tailPrev
  3. free尾结点并置为NULL
  4. 防止只有哨兵位时,将哨兵位free掉

在这里插入图片描述

List.c中代码实现:

void ListPopBack(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);//防止链表中只有哨兵位
	ListNode* tail = phead->prev;
	ListNode* tailPrev = tail->prev;

	free(tail);
	tail == NULL;

	tailPrev->next = phead;
	phead->prev = tailPrev;
}

查找

List.h中声明函数ListNode* ListFind(ListNode* phead, LTDataType x);

查找函数和打印函数类似,只从哨兵位的下一个节点开始,向后寻找至哨兵位结束。

List.c中代码实现:

ListNode* ListFind(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

在pos位置之前插入

List.h中声明函数void ListInsert(ListNode* pos, LTDataType x);

在这里插入图片描述
如图,假设在pos位置之前插入30,
在这里插入图片描述

  1. posPrev 的next指向新节点
  2. 新节点的prev指向posPrev
  3. pos的prev指向新节点
  4. 新节点的next指向pos

在头尾前插入时,因为是双向带头循环链表,头尾的前后都不为空,所以可以正常插入。

List.c中代码实现:

void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newnode = BuyListNode(x);
	ListNode* posPrev = pos->prev;
	posPrev->next = newnode;
	newnode->prev = posPrev;
	pos->prev = newnode;
	newnode->next = pos;
}

头插

在ListInsert函数中发现,头插时只需要将哨兵位的下一个节点传给ListInsert函数,就可以实现头插。

List.h中声明函数void ListPushFront(ListNode* phead, LTDataType x);

List.c中代码实现:

void ListPushFront(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListInsert(phead->next, x);
}

优化尾插

既然头插可以用ListInsert函数实现,那么尾插也是类似的道理,只需要将头结点传给ListInsert函数,就可以实现尾插

List.c中代码实现:

void ListPushBack(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListInsert(phead, x);
}

在pos位置删除

在这里插入图片描述
如图:

  1. free掉pos
  2. posPrev的next指向posNext
  3. posNext的prev指向posPrev

List.c中代码实现:

void ListErase(ListNode* pos)
{
	assert(pos);
	ListNode* posPrev = pos->prev;
	ListNode* posNext = pos->next;
	free(pos);
	posPrev->next = posNext;
	posNext->prev = posPrev;
}

优化尾删

同ListInsert一样,ListErase函数也可以实现尾删。只需要将哨兵位的前一个节点,传给ListErase函数,则完成尾删。

List.c中代码实现:

void ListPopBack(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListErase(phead->prev);
}

头删

头删也是同理,ListErase函数中传入哨兵位的下一个节点,则完成头删

List.c中代码实现:

void ListPopFront(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListErase(phead->next);
}

总结

双向带头循环链表的实现是非常简单,只要实现Insert和Erase这两个函数,其他函数都可以附庸。

List.h

#pragma once

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

typedef int LTDataType;

typedef struct ListNode
{
	LTDataType data;
	struct ListNode* prev;
	struct ListNode* next;
}ListNode;

void ListInit(ListNode** pphead);

ListNode* BuyListNode(LTDataType x);

void ListPushBack(ListNode* phead, LTDataType x);

void ListPopBack(ListNode* phead);

void ListPrint(ListNode* phead);


void ListPushFront(ListNode* phead, LTDataType x);

void ListPopFront(ListNode* phead);

ListNode* ListFind(ListNode* phead, LTDataType x);

void ListInsert(ListNode* pos, LTDataType x);

void ListErase(ListNode* pos);

List.c

#include"list.h"

void ListInit(ListNode** pphead)
{
	*pphead = BuyListNode(0);
	(*pphead)->next = *pphead;
	(*pphead)->prev = *pphead;
}

ListNode* BuyListNode(LTDataType x)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}

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

void ListPushBack(ListNode* phead, LTDataType x)
{
	assert(phead);

	/*ListNode* tail = phead->prev;
	ListNode* newnode = BuyListNode(x);

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

	newnode->next = phead;
	phead->prev = newnode;*/
	ListInsert(phead, x);
}


void ListPopBack(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	/*ListNode* tail = phead->prev;
	ListNode* tailPrev = tail->prev;

	free(tail);
	tail = NULL;

	tailPrev->next = phead;
	phead->prev = tailPrev;*/

	ListErase(phead->prev);
}


ListNode* ListFind(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newnode = BuyListNode(x);
	ListNode* posPrev = pos->prev;
	posPrev->next = newnode;
	newnode->prev = posPrev;
	pos->prev = newnode;
	newnode->next = pos;
}

void ListPushFront(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListInsert(phead->next, x);
}


void ListErase(ListNode* pos)
{
	assert(pos);
	ListNode* posPrev = pos->prev;
	ListNode* posNext = pos->next;
	free(pos);
	posPrev->next = posNext;
	posNext->prev = posPrev;
}

void ListPopFront(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	ListErase(phead->next);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TlowolT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值