//设计单向循环列表
/**********************************************
*file name: circularlinkedlist.c
*author:[email protected]
*date:2024/4/23
*function:设计单向循环列表
*note:None
*CopyRight (c) 2023-2024 邮箱 All Right Reseverd
***********************************************/
//指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
//构造链表的节点,节点链表中所有的数据类型都一样
typedef struct LinkedList
{
DataType_t data; //节点的数据域
struct LinkedList * next;
}CircLList_t;
//创建一个空单向循环链表,头链表里应该有一个头节点,对链表进行初始化
CircLList_t * LList_Create(void)
{
//1.创建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;
}
CircLList_t CircLList_Newnode(DataType_t data)
{
//1.创建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:max
* function functionality:分别使用头插法、尾插法、指定插入三种方法插入新结点
* param:
* @a:LList_t * Head
* @b:DataType_t data
* retuen value: LList_t *
* author:[email protected]
* date:2024/4/2
* 修改历史:
* 版本修改:
1.v1.0.n(1表示主板本,0表示)
2.
3.
······
*/
//头插法
bool LCircLList_HeadInsert(CircLList_t * Head ,DataType_t data)
{
//1.创建新的结点,并对新结点进行初始化(数据域+指针域)
CircLList_t *New = CircLList_Newnode(data);
CircLList_t *Phead = Head;
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//2.判断循环链表是否为空,如果为空则直接插入
if(Head->next == NULL)
{
Head->next = New;
New->next = New;
return ture;
}
//3.如果单项循环链表非空则把新结点插入到循环链表的头部
New->next = head->next;
head->next = New;
!!!遍历找尾结点指向首结点
while(Phead != New->next )
{
//把当前结点的直接后继作为新的当前结点
Phead = Phead->next;
}
Phead->next = New;
return true;
}
//尾插法
bool CircLList_TailInsert(CircLList_t * Head ,DataType_t data)
{
CircLList_t *Phead = Head;
//1.创建新的结点,并对新结点进行初始化(数据域+指针域)
CircLList_t *CircLList_Newnode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//2.判断链表是否为空,如果为空则直接插入
if(Head->next == NULL)
{
Head->next = New;
New->next = New
return ture;
}
//3.如果链表非空则把新结点插入到链表的尾部
while(Phead->next != Head->next )
{
Phead = Phead->next;
}
Phead->next = New;
New->next = Head->next;
printf("nenode Insert success!");
return true;
}
//指定位置插入
bool CircLList_TailInsert(CircLList_t * Head ,DataType_t destval,DataType_t data)
{
CircLList_t *Phead = Head->next;
//1.创建新的结点,并对新结点进行初始化(数据域+指针域)
CircLList_t CircLList_Newnode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
//2.判断链表是否为空,如果为空则直接插入
if(Head->next == NULL)
{
Head->next = New;
New-next = New;
return ture;
}
//遍历链表,目的是找到目标结点,比较结点的数据域
while(Phead != NULL && destval !=Phead->data)
{
//把当前结点的直接后继作为新的当前结点
Phead = Phead->next;
}
if(Phead == NULL)
{
return false;
}
//到这一步说明找到了目标结点,则把新结点加入到目标的后面
New->next = Phead->next;
Phead->next = New;
}
//遍历
bool CircLList_Print(CircLList_Head)
{
//对单向循环链表的头结点的地址进行备份
CircLList_t *Phead = Head;
//判断当前链表是否为空,空则直接退出
if(head->next == Head)
{
printf("输出列表为空\n");
return false;
}
//从头结点开始遍历
while(Phead->next)
{
//把头的直接后继作为新的头节点
Phead = Phead->next;
//输出头结点的直接后继的数据域
printf("data = %d\n",Phead->data);
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if(Phead->next == Head->next)
{
break;
}
}
return ture;
}
//头删
bool CircLList_HeadDel(CircLList_t * Head)
{
CircLList_t *Phead = Head;
if(head->next == Head)
printf("输出列表为空\n");
return false;
//非空的情况
//遍历链表找尾节点
while(Phead->next)
{
Phead = Phead->next;//把头的直接后继作为新的头节点
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if(Phead->next == Head->next)
{
break;
}
}
//将尾结点的next指向首结点的下一个结点
Phead->next = Head->next->next;
head->next = Head->next->next;
free(Head->next);
}
//尾删
//遍历找到尾节点
bool CircLList_HeadDel(CircLList_t * Head)
{
CircLList_t *Phead = Head->next;
CircLList_t *Phead_p = Head;
while(Phead->next)
{
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if(Phead->next == Head->next)
{
break;
}
Phead_p = Phead; //把当前结点的前驱结点向后偏移
Phead = Phead->next;//把头的直接后继作为新的头节点
}
Phead_p->next = Phead->next;
Phead->next = NULL;
free(phead);
}
//删除指定结点
bool CircLList_HeadDel(CircLList_t * Head,DataType_t destval,DataType_t data)
{
CircLList_t *Phead = Head->next;
CircLList_t *Phead_p = Head;
//1.遍历找到待删除结点以及找到目标节点的前驱
while(Phead->next)
{
//比较当前结点的值是否等于目标结点的值
if(Phead->data == destval)
{
break;
}
Phead_p = Phead; //把当前结点的前驱结点向后偏移
Phead = Phead->next;//把头的直接后继作为新的头节点
}
Phead_p->next = Phead->next;//2.待删除结点直接前驱指向直接后继
Phead->next = NULL;//3.将待删除结点的next指向null
free(Phead);//4.free待删除结点
}