单向循环链表的接口程序
目录
单向循环链表
头文件
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
```
链表、节点的创建
/*******************************************************************
*
* file name: 单向循环链表的接口程序
* author : 17647576169@163.com
* date : 2024-4-23
* function :
* note : None
*
* CopyRight (c) 2024 17647576169@163.com All Right Reseverd
*
* *****************************************************************/
// 指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造单向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{
DataType_t data; // 结点的数据域
struct CircularLinkedList *next; // 结点的指针域
} CircLList_t;
/********************************************************************
*
* name : CircLList_Create
* function : 创建一个空的单向循环顺序链表,空链表应该有一个头结点,对链表进行初始化
* argument : None
*
* retval : 返回创建的链表的头地址
* author : 17647576169@163.com
* date : 2024-4-23
* note : None
*
* *****************************************************************/
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;
}
/********************************************************************
*
* name : CircLList_NewNode
* function : 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
* argument : @data:需插入的数据
*
* retval : 返回新创建链表节点的地址
* author : 17647576169@163.com
* date : 2024-4-23
* note : None
*
* *****************************************************************/
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;
}
```
三种节点插入方式
在链表头部插入新节点
在链表尾部插入新节点
在链表中某数据后插入新节点
代码
/********************************************************************
*
* name : CircLList_HeadInsert
* function : 向链表的头部进行数据插入
* argument : @head:目标链表
* @data:需插入的数据
* retval : 返回新创建链表节点的地址
* author : 17647576169@163.com
* date : 2024-4-22
* note : None
*
* *****************************************************************/
bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
// 备份头节点
CircLList_t *Phead = Head;
// 1.创建新的结点,并对新结点进行初始化
CircLList_t *New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (Head == Head->next)
{
Head->next = New;
New->next = New;
return true;
}
// 3.如果链表为非空,则把新结点插入到链表的头部
while (Phead->next)
{
Phead = Phead->next;
if (Phead->next == Head->next)
{
break;
}
}
Phead->next = New; // 尾结点指向新的首节点
New->next = Head->next; // 新节点指向原首节点
Head->next = New; // 头节点指向新的首节点
return true;
}
/********************************************************************
*
* name : CircLList_TailInsert
* function : 向链表的尾部进行数据插入
* argument : @head:目标链表
* @data:需插入的数据
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note : None
*
* *****************************************************************/
bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
// 备份头节点
CircLList_t *Phead = Head;
// 1.创建新的结点,并对新结点进行初始化
CircLList_t *New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (Head == Head->next)
{
Head->next = New;
New->next = New;
return true;
}
// 3.如果链表为非空,则把新结点插入到链表的头部
while (Phead->next)
{
Phead = Phead->next;
if (Phead->next == Head->next)
{
break;
}
}
Phead->next = New; // 尾结点指向新的尾节点
New->next = Head->next; // 新节点指向原首节点
return true;
}
/********************************************************************
*
* name : CircLList_DestInsert
* function : 向链表的指定位置后进行插入
* argument : @head:目标链表
* @data:需插入的数据
* @dest:插入位置
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note : None
*
* *****************************************************************/
bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
// 备份首节点
CircLList_t *Phead = Head->next;
// 1.创建新的结点,并对新结点进行初始化
CircLList_t *New = CircLList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (Head == Head->next)
{
Head->next = New;
New->next = New;
return true;
}
// 目标数据在头部
if (destval == Phead->data)
{
CircLList_HeadInsert(Head, data);
return true;
}
// 找目标数据
CircLList_t *P = NULL; // 定义变量保存找的目标数据的节点地址
while (Head->next != Phead->next)
{
Phead = Phead->next;
if (destval == Phead->data)
{
P = Phead;
break;
}
}
// 找不到目标数据
if (NULL == P)
{
perror("The target data cannot be found\n");
return false;
}
// 目标数据在尾部
if (Head->next == P->next)
{
CircLList_TailInsert(Head, data);
}
// 节点在中间开始插入
else
{
New->next = Phead->next;
Phead->next = New;
}
return true;
}
```
三种删除方式
删除链表头部的节点
链表尾部的节点
删除链表中指定的数据节点
代码
/********************************************************************
*
* name : CircLList_HeadDel
* function : 头删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note : None
*
* *****************************************************************/
bool CircLList_HeadDel(CircLList_t *Head)
{
// 备份首节点地址
CircLList_t *Phead = Head;
// 备份头节点地址
CircLList_t *Temp = Head->next;
// 1.判断链表是否为空,如果为空
if (Head == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 链表只有一首个节点
if (Head->next == Head->next->next)
{
Temp->next = NULL;
Head->next = Head;
free(Temp);
return true;
}
// 遍历到尾结点
while (Phead->next)
{
Phead = Phead->next;
if (Head->next == Phead->next)
{
break;
}
}
Phead->next = Head->next->next; // 尾结点指向原首节点的后继
Head->next = Phead->next; // 头节点指向新的首节点
Temp->next = NULL; // 原首节点指针域初始化
free(Temp);
return true;
}
/********************************************************************
*
* name : CircLList_TailDel
* function : 尾删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note : None
*
* *****************************************************************/
bool CircLList_TailDel(CircLList_t *Head)
{
// 备份首节点地址
CircLList_t *Phead = Head;
// 备份头节点地址
CircLList_t *Temp = Head->next;
// 1.判断链表是否为空,如果为空
if (Head == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 链表只有一首个节点
if (Head->next == Head->next->next)
{
Temp->next = NULL;
Head->next = Head;
free(Temp);
return true;
}
// 遍历到尾结点
while (Phead->next)
{
if (Head->next == Temp->next)
{
break;
}
Phead = Temp; // 记录尾节点前驱
Temp = Temp->next;
}
Temp->next = NULL; // 原尾结点指向首节点
Phead->next = Head->next; // 尾结点的指针域重置
free(Temp); // 释放堆空间
return true;
}
/********************************************************************
*
* name : CircLList_Del
* function : 中删
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool CircLList_Del(CircLList_t *Head, DataType_t data)
{
// 备份首节点地址
CircLList_t *Phead = Head;
// 备份头节点地址
CircLList_t *Temp = Head->next;
// 1.判断链表是否为空,如果为空
if (Head == Head->next)
{
perror("Linked lists have no data\n");
return false;
}
// 删除的节点在头
if (data == Head->next->data)
{
CircLList_HeadDel(Head);
return true;
}
// 遍历到尾结点
CircLList_t *P = NULL; // 定义变量保存找的目标数据的节点地址
while (Temp->next)
{
Temp = Temp->next;
Phead = Phead->next; // 记录尾节点前驱
if (data == Temp->data)
{
P = Temp;
break;
}
}
// 找不到目标数据
if (NULL == P)
{
perror("The target data cannot be found\n");
return false;
}
// 目标数据在尾部
if (Head->next == Phead->next)
{
CircLList_TailDel(Head);
}
else
{
Phead->next = Temp->next; // 删除节点的前驱指向删除节点的后继
Temp->next = NULL; // 删除节点的指针域重置
free(Temp); // 释放堆空间
}
return true;
}
```
遍历链表
/********************************************************************
*
* name : CircLList_Print
* function : 遍历链表
* argument : @head:目标链表
*
*
* retval : none
* author : 17647576169@163.com
* date : 2024-4-22
* note : None
*
* *****************************************************************/
void CircLList_Print(CircLList_t *Head)
{
// 对链表的头文件的地址进行备份
CircLList_t *Phead = Head;
// 首结点
while (Phead->next)
{
// 把头的直接后继作为新的头结点
Phead = Phead->next;
// 输出头结点的直接后继的数据域
printf("data = %d\n", Phead->data);
if (Phead->next == Head->next)
{
break;
}
}
}
```
验证程序
int main(int argc, char const *argv[])
{
// 1.创建顺序表
CircLList_t *Manager = CircLList_Create();
// 2.向顺序表中的尾部插入新元素
CircLList_TailInsert(Manager, 5);
CircLList_TailInsert(Manager, 2);
CircLList_TailInsert(Manager, 1);
CircLList_TailInsert(Manager, 4);
CircLList_TailInsert(Manager, 6);
// 3.遍历顺序表
CircLList_Print(Manager); // -- 5 2 1 4 6
printf("\n");
// 4.向顺序表中的头部插入新元素
CircLList_HeadInsert(Manager, 9);
CircLList_HeadInsert(Manager, 7);
CircLList_HeadInsert(Manager, 8);
CircLList_HeadInsert(Manager, 0);
CircLList_HeadInsert(Manager, 10);
// cha
CircLList_DestInsert(Manager, 8, 0);
// 5.遍历顺序表
CircLList_Print(Manager); // --10 0 8 0 7 9 5 2 1 4 6
printf("\n");
// 头删除
CircLList_HeadDel(Manager);
// 遍历顺序表
CircLList_Print(Manager); // --0 8 0 7 9 5 2 1 4 6
printf("\n");
// 尾删除
CircLList_TailDel(Manager);
// 遍历顺序表
CircLList_Print(Manager); // --0 8 0 7 9 5 2 1 4
printf("\n");
// 删除顺序表的元素
CircLList_Del(Manager, 2);
// 遍历顺序表
CircLList_Print(Manager); // --0 8 7 9 5 1 4 6
printf("\n");
return 0;
return 0;
}
```
输出结果
data = 5
data = 2
data = 1
data = 4
data = 6
data = 10
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 6
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 6
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 1
data = 4
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
暂无评论内容