单向循环链表的接口程序
  6Df3JnWQUC5m 10天前 17 0

单向循环链表的接口程序

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

bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
	// 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.如果链表为非空,则把新结点插入到链表的头部
	New->next = Head->next;
	Head->next = New;
	// 变量链表找到尾结点
	CircLList_t *Phead = Head->next;
	while (Head->next != Phead->next)
	{
		Phead = Phead->next;
	}
	Phead->next = New->next;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_TailInsert
 *	function :  向链表的尾部进行数据插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
	// 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对链表的头文件的地址进行备份
	CircLList_t *Phead = Head->next;
	// 4遍历链表找到尾部
	while (Head->next != Phead->next)
	{
		// 把头的直接后继作为新的头结点
		Phead = Phead->next;
	}
	// 5.把新结点插入到链表的尾部
	Phead->next = New;
	New->next = Head->next;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_TailInsert
 *	function :  向链表的指定位置后进行插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *				@dest:插入位置
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
	// 1.创建新的结点,并对新结点进行初始化
	CircLList_t *New = CircLList_NewNode(data);

	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}

	// 2.判断链表是否为空,如果为空,则直接插入即可
	if (NULL == Head->next)
	{
		Head->next = New;
		New->next = New;
		return true;
	}

	// 备份头节点地址
	CircLList_t *Phead = Head->next;
	CircLList_t *P = NULL;
	// 目标数据在头部
	if (data == Phead->data)
	{
		CircLList_HeadInsert(Head, data);
		return true;
	}
	// 找目标数据
	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 == Phead->next)
	{
		LList_TailInsert(Head, data);
		return true;
	}
	// 开始插入
	New->next = Phead->next;
	Phead->next = New;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  头删
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool CircLList_HeadDel(CircLList_t *Head, DataType_t data)
{
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 备份首节点地址
	CircLList_t *Phead = Head->next;

	// 备份首节点地址
	CircLList_t *Phead2 = Head->next;
	// 链表只有一个节点
	if (Head->next == Phead->next)
	{
		Head->next = Head;
		Phead->next = NULL;
		free(Phead);
		return true;
	}
	// 遍历找到尾结点
	while (Head->next != Phead->next)
	{
		Phead = Phead->next;
	}
	Phead->next = Head->next->next;
	Head->next = Head->next->next;
	Phead2->next = NULL;
	free(Phead2);
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  尾删
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
bool CircLList_TailDel(CircLList_t *Head, DataType_t data)
{
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 备份首节点地址
	CircLList_t *Phead = Head->next;

	// 链表只有一个节点
	if (Head->next == Phead->next)
	{
		Head->next = Head;
		Phead->next = NULL;
		free(Phead);
		return true;
	}
	// 备份首节点后继节点地址
	CircLList_t *Phead2 = Head->next->next;
	// 遍历找到尾结点
	while (Head->next != Phead2->next)
	{
		Phead = Phead->next;
		Phead2 = Phead2->next;
	}
	Phead->next = Head->next;
	Phead2->next = NULL;
	free(Phead2);
	return true;
}
/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	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)
{
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 备份首节点地址
	CircLList_t *Phead = Head->next;

	// 备份首节点地址

	// 删除的节点在头
	if (Head->next->data == Phead->data)
	{
		CircLList_HeadDel(Head, data);
		return true;
	}
	// 遍历到尾结点

	CircLList_t *P = NULL;
	// 备份头节点地址
	CircLList_t *Phead2 = Head;
	while (Head->next != Phead->next)
	{
		Phead = Phead->next;
		Phead2 = Phead2->next;
		if (data == Phead->data)
		{
			P = Phead;
			break;
		}
	}

	// 找不到目标数据
	if (NULL == P)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 目标数据在尾部
	if (Head->next->data == Phead->data)
	{
		CircLList_TailDel(Head, data);
		return true;
	}
	Phead2->next = Phead->next;
	Phead->next = NULL;
	free(Phead);
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_Print
 *	function :  遍历链表
 *	argument :	@head:目标链表
 *
 *
 *	retval	 :  none
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

void LList_Print(CircLList_t *Head)
{
	// 对链表的头文件的地址进行备份
	CircLList_t *Phead = Head;

	// 首结点
	while (Phead->next)
	{
		// 把头的直接后继作为新的头结点
		Phead = Phead->next;

		// 输出头结点的直接后继的数据域
		printf("data = %d\n", Phead->data);
	}
}


​```
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 10天前 0

暂无评论

推荐阅读
  H5oyQecqjP4R   12天前   34   0   0 C语言
  TfjjLvnv5b8J   13天前   19   0   0 C语言
6Df3JnWQUC5m