文章目录
上一篇文章中我们讲解了不带头的单链表的增删查改的功能,这篇文章中我们来讲解带有哨兵位(带头)且是双向循环的链表的增删查改以及一些功能的实现。
这里先用一个图来解释什么是 带头双向循环链表:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/08b991e22ad315202f51a69463a67ea7.png)
这个链表每个节点都有两个指针,分别是前驱和后继,分别指向前一个节点和后一个节点,这里的head就是头,也叫哨兵位,它不存放数据,只有两个指针,那循环又是什么意思呢?从这个图来说就是d3的next指针指向head,head的prev指针指向d3。
结构相对于单链表更加复杂,但是能让我们更简单的实现,下面我们就来实现它的功能,相同的还是把程序分为声明的头文件,函数实现的.c文件和测试的.c文件。
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* next;
struct ListNode* prev;
}LST;
LST* BuyLTNode(LTDataType x);// 创建返回链表的头结点.
void ListDestory(LST* phead);// 双向链表销毁
void ListPrint(LST* phead);// 双向链表打印
void ListPushBack(LST* phead, LTDataType x);// 双向链表尾插
void ListPopBack(LST* phead);// 双向链表尾删
void ListPushFront(LST* phead, LTDataType x);// 双向链表头插
void ListPopFront(LST* phead);// 双向链表头删
LST* ListFind(LST* phead, LTDataType x);// 双向链表查找
void ListInsert(LST* pos, LTDataType x);// 双向链表在pos的前面进行插入
void ListErase(LST* pos);// 双向链表删除pos位置的节点
//创建一个新的节点,注意现在有两个指针了
LST* BuyLTNode(LTDataType x)
{
LST* newnode = (LST*)malloc(sizeof(LST));
if (newnode == NULL)
{
perror("malloc failed");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
//销毁链表,最后不用把phead置空,因为出了函数就销毁了。
void ListDestory(LST* phead)
{
assert(phead);
LST* cur = phead->next;
while (cur != phead)
{
LST* del = cur->next;
free(cur);
cur = del;
}
free(phead);
}
//打印,注意边界条件
void ListPrint(LST* phead)
{
assert(phead);
printf("phead<=>");
LST* cur = phead->next;
while (cur!=phead)
{
printf("%d<->", cur->data);
cur = cur->next;
}
printf("\n");
}
//尾插,这里注意要改变四个指针
void ListPushBack(LST* phead, LTDataType x)
{
assert(phead);
LST* tail = phead->prev;
LST* newnode = BuyLTNode(x);
tail->next = newnode;
newnode->prev = tail;
phead->prev = newnode;
newnode->next = phead;
}
//尾删,这里要注意需要多断言assert(phead->next!=phead);
,因为如果这个链表只有一个哨兵位,也无法进行尾删.
void ListPopBack(LST* phead)
{
assert(phead);
assert(phead->next!=phead);
LST* tail = phead->prev;
LST* tailprev = tail->prev;
free(tail);
phead->prev = tailprev;
tailprev->next = phead;
}
//头插,同前面一样需要注意先要把phead的next存储起来,不然会造成覆盖,无法找到phead的下一个节点.
void ListPushFront(LST* phead, LTDataType x)
{
assert(phead);
LST* newnode = BuyLTNode(x);
LST*tail=phead->next;
phead->next = newnode;
newnode->next = tail;
newnode->prev = phead;
tail->prev = newnode;
}
//头删 这里注意需要保存两个指针,不然也会覆盖找不到
void ListPopFront(LST* phead)
{
assert(phead);
assert(phead->next != phead);
LST* tail = phead->next;
LST* cur = tail->next;
phead->next = cur;
cur->prev = phead;
free(tail);
}
//查找
LST* ListFind(LST* phead, LTDataType x)
{
assert(phead);
LST* cur = phead->next;
while (cur!=phead)
{
if (cur->data== x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos的前面进行插入
void ListInsert(LST* pos, LTDataType x)
{
assert(pos);
LST* newnode = BuyLTNode(x);
LST* posprev = pos->prev;
posprev->next = newnode;
newnode->prev = posprev;
newnode->next = pos;
pos->prev = newnode;
}
//删除pos位置的节点
void ListErase(LST* pos)
{
assert(pos);
LST* posprev = pos->prev;
LST* posnext = pos->next;
posprev->next = posnext;
posnext->prev = posprev;
free(pos);
}
当然写完了插入删除后我们就可以把它们复用到头插和尾插,头删和尾删当中:
头插:LTInsert(phead->next, x);
尾插:LTInsert(phead, x);
相当于在phead的前面插入一个节点
头删:LTErase(phead->next);
尾删:LTErase(phead->prev);
以上就是双向带头循环链表功能的实现,如有错误欢迎指正,谢谢大家!