单向循环链表的接口程序

单向循环链表的接口程序

单向循环链表

image

头文件

#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;
}
​```

三种节点插入方式

在链表头部插入新节点

image

在链表尾部插入新节点

image

在链表中某数据后插入新节点

image

代码

/********************************************************************
 *
 *	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;
}
​```

三种删除方式

删除链表头部的节点

image

链表尾部的节点

image

删除链表中指定的数据节点

image

代码

/********************************************************************
 *
 *	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

posted @ 2024-04-24 06:44  谁TM买小米啊  阅读(9)  评论(0编辑  收藏  举报