/**
- @file name: main.c
- @brief 单向循环链表的接口程序
- @author [email protected]
- @date 2024/04/23
- @version 1.0 :版本
- @property :属性介绍
- @note 补充 注意 说明
- CopyRight (c) 2023-2024 [email protected] All Right Reseverd
*/
include <stdio.h>
include <stdbool.h>
// 指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造单向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{
DataType_t data; // 结点的数据域
struct CircLList_t *next; // 结点的指针域
} CircLList_t;
// 创建一个空单向循环链表,空链表应该有一个头结点,对链表进行初始化
/**
-
@function name: CircLList_Create
-
@brief 创建新的头结点并初始化
-
@param 介绍函数参数 void
-
@retval DataType_t *
-
@date 2024/04/22
-
@version 1.0 :版本
-
@note 补充 注意 说明
*/
CircLList_t *CircLList_Create(void)
{
// 1.创建一个头结点并对头结点申请内存
CircLList_t *Head = (CircLList_t *)calloc(1, sizeof(CircLList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身,体现“循环”思想
Head->next = Head;// 3.把头结点的地址返回即可
return Head;
}
// 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
/**
-
@function name: CircLList_NewNode
-
@brief 创建新的结点
-
@param 介绍函数参数 void
-
@retval DataType_t *
-
@date 2024/04/22
-
@version 1.0 :版本
-
@note 补充 注意 说明
*/
CircLList_t *CircLList_NewNode(DataType_t data)
{
// 1.创建一个新结点并对新结点申请内存
CircLList_t *New = (CircLList_t *)calloc(1, sizeof(CircLList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}// 2.对新结点的数据域和指针域进行初始化
New->data = data;
New->next = NULL;return New;
}
// 头插
/**
- @function name: CircLList_HeadInsert
- @brief 在链表头部进行插入
- @param 介绍函数参数 @Head
-
@data
- @retval bool
- @date 2024/04/22
- @version 1.0 :版本
- @note 补充 注意 说明
*/
bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
CircLList_t *Phead = Head;
// 判断链表是否为空
CircLList_t *New = CircLList_NewNode(data);
if (NULL == Head)
{
Head->next = New;
New->next = New;
return true;
}
// 遍历链表找到尾结点
while (Phead->next != Head->next)
{
Phead = Phead->next;
}
New->next = Head->next;
Head->next = New;
Phead->next = New;
return true;
}
// 尾插
/**
- @function name: CircLList_TailInsert
- @brief 在链表尾部进行插入
- @param 介绍函数参数 @Head
-
@data
- @retval bool
- @date 2024/04/22
- @version 1.0 :版本
- @note 补充 注意 说明
*/
bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
CircLList_t *Phead = Head;
// 判断链表是否为空
CircLList_t *New = CircLList_NewNode(data);
if (NULL == Head)
{
Head->next = New;
New->next = New;
return true;
}
// 遍历链表找到尾结点
while (Phead->next != Head->next)
{
Phead = Phead->next;
}
Head->next = New->next;
Phead->next = New;
return true;
}
// 指定位置插入
/**
- @function name: CircLList_DestInsert
- @brief 在链表任意部位进行插入
- @param 介绍函数参数 @Head
-
@data
-
@dest
- @retval bool
- @date 2024/04/22
- @version 1.0 :版本
- @note 补充 注意 说明
*/
bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
CircLList_t *Phead = Head;
// 判断链表是否为空
CircLList_t *New = CircLList_NewNode(data);
if (NULL == Head)
{
return false;
}
// 遍历链表找到目标结点
while (Phead->data && Phead->data != destval)
{
Phead = Phead->next;
break;
}
if (Phead->next == Head->next)
{
CircLList_TailInsert(Head, data);
}
New->next = Phead->next;
Phead->next = New;
return true;
}
// 遍历链表
bool CircLList_Print(CircLList_t *Head)
{
// 对单向循环链表的头结点的地址进行备份
CircLList_t *Phead = Head;
// 判断当前链表是否为空,为空则直接退出
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
// 从首结点开始遍历
while (Phead->next)
{
// 把头结点的直接后继作为新的头结点
Phead = Phead->next;
// 输出头结点的直接后继的数据域
printf("data = %d\n", Phead->data);
// 判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if (Phead->next == Head->next)
{
break;
}
}
return true;
}
// 首删
bool LList_HeadDel(CircLList_t *Head)
{
// 对单向循环链表的头结点的地址进行备份
CircLList_t *Phead1 = Head;
CircLList_t *Phead2 = Head->next;
if (Head == Head->next)
{
printf("the list is empty,can not delate");
return false;
}
while (Phead1->next != Head->next)
{
Phead1 = Phead1->next;
}
Phead1->next = Phead2->next;
Head->next = Phead2->next;
Phead2->next = NULL;
free(Phead2);
}
// 尾删
bool LList_TialDel(CircLList_t *Head)
{
CircLList_t *Phead1 = Head;
CircLList_t *Phead2 = Head->next;
int count = 0;
if (Head == Head->next)
{
printf("the list is empty,can not delate");
return false;
}
while (Phead1->next != Head->next)
{
Phead1 = Phead1->next;
count++;
}
for (size_t i = 0; i < count - 1; i++)
{
Phead2 = Phead2->next;
}
Phead2->next = Head->next;
Phead1->next = NULL;
free(Phead1);
return true;
}
// 中间删
bool LList_DestDel(CircLList_t *Head, DataType_t destval)
{
CircLList_t *Phead1 = Head;
CircLList_t *Phead2 = Head->next;
int count = 0;
if (Head == Head->next)
{
printf("the list is empty,can not delate");
return false;
}
if (Phead1->next == Phead1->next)
{
Head->next = Head->next;
Phead2->next = NULL;
free(Phead2);
return true;
}
while (Phead1->data && Phead1->data != destval)
{
Phead1 = Phead1->next;
count++;
break;
}
if (Phead1->next == Head->next)
{
LList_HeadDel(Head);
}
for (size_t i = 0; i < count - 1; i++)
{
Phead2 = Phead1->next;
}
Phead2->next = Phead1->next;
Phead1->next = NULL;
free(Phead1);
return true;
}
int main(int argc, char const *argv[])
{
return 0;
}
标签:Head,单向,结点,CircLList,next,链表,Phead,接口 From: https://www.cnblogs.com/zeratul/p/18153671