【数据结构】带头双向循环链表的实现

🌇个人主页:平凡的小苏

📚学习格言:别人可以拷贝我的模式,但不能拷贝我不断往前的激情

🛸C语言专栏:https://blog.csdn.net/vhhhbb/category_12174730.html

🚀数据结构专栏:https://blog.csdn.net/vhhhbb/category_12211053.html

        家人们更新不易,你们的👍点赞👍和⭐关注⭐真的对我真重要,各位路过的友友麻烦多多点赞关注,欢迎你们的私信提问,感谢你们的转发!

        关注我,关注我,关注我,你们将会看到更多的优质内容!!

 

       

1、带头循环双向链表

        我们在单链表中,有了next指针,这使得我们要查找下一节点的时间复杂度为O(1)。可是如果我们要查找的是上一节点的话,那最坏的时间复杂度就是O(n)了,因为我们每次都要从头开始遍历查找。

        为了克服单向性这一缺点,我们的老科学家们,设计出了双向链表。双向链表是在单链表的每个结点中,再设置一个指向其前驱结点的指针域。所以再双向链表中的结点都有两个指针域,一个指向直接后继,另一个指向直接前驱。

        既然单链表可以有循环链表,那么双向链表也可以有循环双向链表。(如下图所示)

2、双向循环链表函数接口的实现

2.1、双向循环链表的结构

typedef int LTDataType;
typedef struct ListNode
{
	LTDataType _data;//数据
	struct ListNode* _next;//后继指针
	struct ListNode* _prev;//前驱指针
}ListNode;

2.2、初始化双向循环链表

ListNode* ListCreate()
{
	ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
	if (phead == NULL)
	{
		perror("malloc Fail:");
		exit(-1);
	}
	phead->_next = phead;
	phead->_data = -1;
	phead->_prev = phead;
	return phead;
}

由于是带头循环链表,我们需要malloc一个头节点出来,当链表是空的时候,前驱指针和后继指针都指向头结点。

2.3、双向循环链表的插入 

// 创建返回链表的头结点.
ListNode* BuyListNode(LTDataType x)
{
	ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
	if (newhead == NULL)
	{
		perror("malloc fail:");
		exit(-1);
	}
	newhead->_data = x;
	newhead->_next = NULL;
	newhead->_prev = NULL;
	return newhead;
}
//双链表插入
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newhead = BuyListNode(x);//该函数是创建新节点的函数
	ListNode* Prev = pos->_prev;
	Prev->_next = newhead;
	newhead->_prev = Prev;
	newhead->_next = pos;
	pos->_prev = newhead;
}

注:由于我们是在pos的前面插入一个结点,那么我们就应该保存上一个结点。

插入算法的具体操作步骤:

        1.Prev->_next = newhead;

        2.newhead->_prev = Prev;

        3.newhead->_next = pos;

        4.pos->_prev = newhead;

2.4、双向循环链表的删除操作

// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
	assert(pos);//删除前pos不能为空
	assert(!ListEmpty(pos));//链表不为空才能删
	ListNode* ne = pos->_next;//保存pos位置的后一个结点
	pos->_prev->_next = ne;//删除结点的具体操作
	ne->_prev = pos->_prev;
	free(pos);//释放
}

2.5、双向循环链表的判空

bool ListEmpty(ListNode* pHead)
{
	assert(pHead);
	return pHead->_next == pHead;如果头结点的下一个结点也等于头结点的话那么链表为空
}

2.6、双向循环链表的打印

// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)
	{
		printf("%d ", cur->_data);
		cur = cur->_next;
	}
	printf("\n");
}

2.7、双向循环链表的销毁

// 双向链表销毁
void ListDestory(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)//链表要遍历释放
	{
		ListNode* ne = cur->_next;
		free(cur);
		cur = ne;
	}
	free(pHead);
	pHead = NULL;
}

3、源代码

由于头插、头删、尾插、尾删可以用双向循环链表的插入和删除操作复用,这里直接放置源代码。

3.1、DList.c 

#include"DSList.h"
// 创建返回链表的头结点.
ListNode* BuyListNode(LTDataType x)
{
	ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
	if (newhead == NULL)
	{
		perror("malloc fail:");
		exit(-1);
	}
	newhead->_data = x;
	newhead->_next = NULL;
	newhead->_prev = NULL;
	return newhead;
}
ListNode* ListCreate()
{
	ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
	if (phead == NULL)
	{
		perror("malloc Fail:");
		exit(-1);
	}
	phead->_next = phead;
	phead->_data = -1;
	phead->_prev = phead;
	return phead;
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newhead = BuyListNode(x);
	ListNode* tail = pHead->_prev;
	newhead->_prev = tail;
	tail->_next = newhead;
	newhead->_next = pHead;
	pHead->_prev = newhead;
	//ListInsert(pHead, x);
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);
	ListNode* newhead = BuyListNode(x);
	ListNode* first = pHead->_next;
	newhead->_prev = pHead;
	pHead->_next = newhead;
	newhead->_next = first;
	first->_prev = newhead;
	//ListInsert(pHead->_next, x);
}
// 双向链表在pos的前面进行插入

//判空
bool ListEmpty(ListNode* pHead)
{
	assert(pHead);
	return pHead->_next == pHead;
}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	assert(!ListEmpty(pHead));
	ListNode* tail = pHead->_prev;
	ListNode* prevtail = tail->_prev;
	prevtail->_next = pHead;
	pHead->_prev = prevtail;
	free(tail);
	//ListErase(pHead->_prev);
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	assert(!ListEmpty(pHead));
	ListNode* first = pHead->_next;
	pHead->_next = first->_next;
	first->_next->_prev = pHead;
	free(first);
	//ListErase(pHead->_next);
}
//双链表插入
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);
	ListNode* newhead = BuyListNode(x);
	ListNode* Prev = pos->_prev;
	Prev->_next = newhead;
	newhead->_prev = Prev;
	newhead->_next = pos;
	pos->_prev = newhead;
}
// 双向链表查找
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位置的节点
void ListErase(ListNode* pos)
{
	assert(pos);
	assert(!ListEmpty(pos));
	ListNode* ne = pos->_next;
	pos->_prev->_next = ne;
	ne->_prev = pos->_prev;
	free(pos);
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)
	{
		printf("%d ", cur->_data);
		cur = cur->_next;
	}
	printf("\n");
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->_next;
	while (cur != pHead)
	{
		ListNode* ne = cur->_next;
		free(cur);
		cur = ne;
	}
	free(pHead);
	pHead = NULL;
}

3.2、DList.h

#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<stdlib.h>
// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
	LTDataType _data;
	struct ListNode* _next;
	struct ListNode* _prev;
}ListNode;

// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
//判空
bool ListEmpty(ListNode* pHead);

好了!!!小编的分享到这里就结束了,有什么不足的地方请大佬多多指教!

  • 31
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论
以下是Java实现带头双向循环链表的完整源码,供参考: ``` public class DoublyCircularLinkedList<T> { private Node<T> head; // 头节点 // 节点类 private static class Node<T> { T data; Node<T> prev; Node<T> next; Node(T data) { this.data = data; this.prev = null; this.next = null; } } // 构造函数 public DoublyCircularLinkedList() { head = new Node<>(null); head.prev = head; head.next = head; } // 在链表末尾添加元素 public void add(T data) { Node<T> node = new Node<>(data); node.prev = head.prev; node.next = head; head.prev.next = node; head.prev = node; } // 在指定位置插入元素 public void insert(int index, T data) { Node<T> node = new Node<>(data); Node<T> p = head.next; int i = 0; while (p != head && i < index) { p = p.next; i++; } if (p == head || i > index) { throw new IndexOutOfBoundsException(); } node.prev = p.prev; node.next = p; p.prev.next = node; p.prev = node; } // 删除指定位置的元素 public void remove(int index) { Node<T> p = head.next; int i = 0; while (p != head && i < index) { p = p.next; i++; } if (p == head || i > index) { throw new IndexOutOfBoundsException(); } p.prev.next = p.next; p.next.prev = p.prev; p.prev = null; p.next = null; } // 获取指定位置的元素 public T get(int index) { Node<T> p = head.next; int i = 0; while (p != head && i < index) { p = p.next; i++; } if (p == head || i > index) { throw new IndexOutOfBoundsException(); } return p.data; } // 获取链表长度 public int size() { Node<T> p = head.next; int size = 0; while (p != head) { size++; p = p.next; } return size; } } ``` 该代码实现带头双向循环链表数据结构,支持在链表末尾添加元素、在指定位置插入元素、删除指定位置的元素、获取指定位置的元素、获取链表长度等操作。在算法实现中,通过一个Node类来表示链表中的节点,包含数据域、前驱指针和后继指针。同时,链表的头节点也是一个Node对象,通过头节点来连接链表的首尾,形成双向循环链表

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

平凡的小苏

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

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

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

打赏作者

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

抵扣说明:

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

余额充值