#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
// 指的是双向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造双向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleCirLList
{
DataType_t data; // 结点的数据域
struct DoubleCirLList *prev; // 直接前驱的指针域
struct DoubleCirLList *next; // 直接后继的指针域
} DoubleCirLList_t;
// 创建一个空双向循环链表,空链表应该有一个头结点,对链表进行初始化
DoubleCirLList_t *DoubleCirLList_Create(void)
{
// 1.创建一个头结点并对头结点申请内存
DoubleCirLList_t *Head = (DoubleCirLList_t *)calloc(1, sizeof(DoubleCirLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身即可,体现“循环”
Head->prev = Head;
Head->next = Head;
// 3.把头结点的地址返回即可
return Head;
}
// 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
DoubleCirLList_t *DoubleCirLList_NewNode(DataType_t data)
{
// 1.创建一个新结点并对新结点申请内存
DoubleCirLList_t *New = (DoubleCirLList_t *)calloc(1, sizeof(DoubleCirLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
// 2.对新结点的数据域和指针域(2个)进行初始化,指针域指向结点自身,体现“循环”
New->data = data;
New->prev = New;
New->next = New;
return New;
}
/**
* @function name:DoubleCirLList_HeadInsert
* @brief :头部插入新元素
* @param :@DoubleCirLList_t *Head :地址
* @DataType_t data :数据域的值
* @retval :bool
* @date :2024/04/25
* @version :1.0 :版本
* @note :
*/
// 头插
bool DoubleCirLList_HeadInsert(DoubleCirLList_t *Head, DataType_t data)
{
// 备份首结点
DoubleCirLList_t *phead = Head->next;
// 创建一个新的结点,并对新结点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (Head->next == Head) // 判断链表是否为空,为空直接退出
{
Head->next = New;
return true;
}
// 不止一个结点的时候,插入新结点
phead->prev->next = New;
New->prev = phead->prev;
New->next = phead;
phead->prev = New;
Head->next = New;
return true;
}
/**
* @function name:DoubleCirLList_TailInsert
* @brief :尾插入新结点
* @param :@DoubleCirLList_t *Head :地址
* @DataType_t data :数据域的值
* @retval :bool
* @date :2024/04/25
* @version :1.0 :版本
* @note :
*/
// 尾插
bool DoubleCirLList_TailInsert(DoubleCirLList_t *Head, DataType_t data)
{
// 备份尾结点
DoubleCirLList_t *phead = Head->next->prev;
// 创建一个新的结点,并对新结点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (Head->next == Head) // 判断链表是否为空,为空直接退出
{
Head->next = New;
return true;
}
// 新结点插入原来的尾结点后面
New->prev = phead;
phead->next = New;
New->next = Head->next;
Head->next->prev = New;
return true;
}
/**
* @function name:DoubleCirLList_DestInsert
* @brief :中间插入结点
* @param :@DoubleCirLList_t *Head :地址
* @DataType_t destval :目标值
* @DataType_t data :数据域的值
* @retval :bool
* @date :2024/04/25
* @version :1.0 :版本
* @note :
*/
// 中间插
bool DoubleCirLList_DestInsert(DoubleCirLList_t *Head, DataType_t destval, DataType_t data)
{
// 备份首结点
DoubleCirLList_t *phead = Head->next;
// 创建一个新的结点,并对新结点进行初始化
DoubleCirLList_t *New = DoubleCirLList_NewNode(data);
if (Head->next == Head) // 判断链表是否为空,为空直接退出
{
printf("The list is empty\n");
return false;
}
// 找到目标值结点,没找到就直接退出
while (phead->data != destval)
{
phead = phead->next;
if (phead == Head->next)
{
printf("There is no destination value.\n");
return false;
}
}
// 找到目标值结点,
New->next = phead->next;
phead->next->prev = New;
New->prev = phead;
phead->next = New;
return true;
}
/**
* @function name:DoubleCirLList_HeadDel
* @brief :头部删除结点
* @param :@DoubleCirLList_t *Head :地址
*
* @retval :bool
* @date :2024/04/25
* @version :1.0 :版本
* @note :
*/
// 头删
bool DoubleCirLList_HeadDel(DoubleCirLList_t *Head)
{
// 备份首结点
DoubleCirLList_t *phead = Head->next;
if (Head->next == Head) // 判断链表是否为空,为空直接退出
{
printf("current linkeflist is empty\n");
return false;
}
if (Head->next->next == Head->next) // 判断是否只有一个结点
{
Head->next = Head;
phead->next = NULL;
phead->prev = NULL;
free(phead);
}
else // 目标结点是首结点,目标结点后面有结点
{
phead->prev->next = phead->next;
phead->next->prev = phead->prev;
Head->next = phead->next;
phead->next = NULL;
phead->prev = NULL;
free(phead);
}
return true;
}
/**
* @function name:DoubleCirLList_TailDel
* @brief :删除尾结点
* @param :@DoubleCirLList_t *Head :地址
*
* @retval :bool
* @date :2024/04/25
* @version :1.0 :版本
* @note :
*/
// 尾删
bool DoubleCirLList_TailDel(DoubleCirLList_t *Head)
{
// 备份首节点
DoubleCirLList_t *phead = Head->next->prev;
if (Head->next == Head) // 判断链表是否为空,为空直接退
{
printf("current linkeflist is empty!\n");
return false;
}
if (Head->next->next == Head->next) // 判断是否只有一个结点
{
phead->next = NULL;
phead->prev = NULL;
Head->next = Head;
free(phead);
}
else // 两个以上的结点
{
phead->prev->next = Head->next;
Head->next->prev = phead->prev;
phead->next = NULL;
phead->prev = NULL;
free(phead);
}
return true;
}
/* @function name:DoubleCirLList_MidDel
* @brief :中间删除目标结点结点
* @param :@DoubleCirLList_t *Head :地址
* @DataType_t destval :目标值
* @retval :bool
* @date :2024/04/25
* @version :1.0 :版本
* @note :
*/
// 中间删
bool DoubleCirLList_MidDel(DoubleCirLList_t *Head, DataType_t destval)
{
// 备份首结点
DoubleCirLList_t *phead = Head->next;
if (Head->next == Head) // 判断链表是否为空,为空直接退
{
printf("current linkeflist is empty!\n");
return false;
}
// 遍历寻找目标值结点
while (phead->data != destval)
{
phead = phead->next;
// 遍历完也没找到
if (phead == Head->next)
{
printf("There is no destination value1111.");
return false;
}
}
// 删除的目标结点是首结点
if (phead == Head->next)
{
// 只有一个首结点,并且只有一个结点
if (phead->next == Head->next)
{
Head->next = phead;
phead->prev = NULL;
phead->next = NULL;
}
else // 多个结点
{
phead->prev->next = phead->next;
phead->next->prev = phead->next;
Head->next = phead->next;
phead->prev = NULL;
phead->next = NULL;
}
free(phead);
}
else // 中间删
{
phead->prev->next = phead->next;
phead->next->prev = phead->prev;
phead->prev = NULL;
phead->next = NULL;
free(phead);
}
return true;
}
// 遍历
bool DoubleCirLList_Print(DoubleCirLList_t *Head)
{
// 备份首节点
DoubleCirLList_t *phead = Head->next;
if (Head->next == Head) // 判断链表是否为空,为空直接退
{
printf("current linkeflist is empty!\n");
return false;
}
// 遍历到尾结点
do
{
printf("data = %d ", phead->data);
phead = phead->next;
} while (phead != Head->next);
return true;
}
//验证
int main(int argc, char const *argv[])
{
DoubleCirLList_t *H = DoubleCirLList_Create();
DoubleCirLList_TailInsert(H, 10);
DoubleCirLList_TailInsert(H, 20);
DoubleCirLList_HeadInsert(H, 30);
DoubleCirLList_HeadInsert(H, 50);
DoubleCirLList_DestInsert(H, 30, 40);
DoubleCirLList_Print(H); // 测试插入功能函数
DoubleCirLList_HeadDel(H);
DoubleCirLList_Print(H); // 测试删除首元素
DoubleCirLList_TailDel(H);
DoubleCirLList_Print(H); // 测试删除尾元素
DoubleCirLList_MidDel(H, 10);
DoubleCirLList_Print(H); // 测试删除目标元素
return 0;
}
```c