//头插
bool CircLList_HeadInsert(CircLList_t *Head,DataType_t data)
{
//创建一个新节点
CircLList_t *New = CircLList_NewNode(data);
//对单向循环链表的头结点的地址进行备份
CircLList_t *Phead = Head;
//判断当前链表是否为空,就直接插入
if (Head->next == Head)
{
Head->next = New;
return true;
}
//从首结点开始遍历
while(Phead->next)
{
//把头结点的直接后继作为新的头结点
Phead = Phead->next;
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if (Phead->next == Head->next)
{
New->next = Head->next;
Head->next = New;
Phead->next = Head->next;
break;
}
}
}
//尾插
bool CircLList_TailInsert(CircLList_t *Head,DataType_t data)
{
//创建一个新节点
CircLList_t *New = CircLList_NewNode(data);
//对单向循环链表的头结点的地址进行备份
CircLList_t *Phead = Head;
//判断当前链表是否为空,就直接插入
if (Head->next == Head)
{
Head->next = New;
return true;
}
//从首结点开始遍历
while(Phead->next)
{
//把头结点的直接后继作为新的头结点
Phead = Phead->next;
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if (Phead->next == Head->next)
{
Phead->next = New;
New->next = Head->next;
break;
}
}
}
//指定位置插入
bool CircLList_DestInsert(CircLList_t *Head,DataType_t destval,DataType_t data)
{
//创建一个新节点
CircLList_t *New = CircLList_NewNode(data);
//对单向循环链表的头结点的地址进行备份
CircLList_t *Phead = Head;
//判断当前链表是否为空,就直接插入
if (Head->next == Head)
{
Head->next = New;
return true;
}
//从首结点开始遍历
while(Phead->next)
{
//把头结点的直接后继作为新的头结点
Phead = Phead->next;
//判断是否找到目标数据,找到了新节点连接目标节点的下一个节点,目标节点再连接新节点
if (New->data == destval)
{
New->next = Phead->next;
Phead->next = New;
break;
}
}
}
//头删
bool LList_HeadDel(CircLList_t *Head)
{
//对单向循环链表的头结点的地址进行备份
CircLList_t *Phead = Head;
//对单向循环链表的首节点再进行备份
CircLList_t *lhead = Head->next;
//判断当前链表是否为空,为空则直接退出
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
//从首结点开始遍历
while(Phead->next)
{
//把头结点的直接后继作为新的头结点
Phead = Phead->next;
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if (Phead->next == Head->next)
{
Phead->next = lhead->next;
Head->next = lhead->next;
lhead->next = NULL;
free(lhead);
break;
}
}
}
//尾删
bool LList_TailDel(CircLList_t *Head)
{
//对单向循环链表的头结点的地址进行备份
CircLList_t *Pphead = Head;
//对单向循环链表的首节点再进行备份
CircLList_t *Phead = Pphead->next;
//判断当前链表是否为空,为空则直接退出
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
while(Phead->next)
{
//把头结点的直接后继作为新的头结点
Phead = Phead->next;
//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
if (Phead->next == Head->next)
{
Pphead->next = Head->next;
Phead->next = NULL;
free(Phead);
break;
}
}
}
//中间删除
bool LList_MiddDel(CircLList_t *Head,DataType_t data)
{
//对单向循环链表的头结点的地址进行备份
CircLList_t *Pphead = Head;
//对单向循环链表的首节点再进行备份
CircLList_t *Phead = Pphead->next;
//判断当前链表是否为空,为空则直接退出
if (Head->next == Head)
{
printf("current linkeflist is empty!\n");
return false;
}
while(Phead->next)
{
//把头结点的直接后继作为新的头结点
Phead = Phead->next;
//判断是否到达找到想要删除的目标数据
if (Phead->data != data)
{
printf("没有找到想要删除的文件!\n");
return false;
}
//找到达找到想要删除的目标数据
if (Phead->data == data)
{
Pphead->next = Phead->next;
Phead->next =NULL;
free(Phead);
break;
}
}
}
标签:Head,单向,结点,CircLList,next,链表,循环,Phead
From: https://www.cnblogs.com/-110/p/18153809