单向循环链表的接口程序

这篇具有很好参考价值的文章主要介绍了单向循环链表的接口程序。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

单向循环链表的接口程序

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


​```

文章来源地址https://www.toymoban.com/news/detail-856748.html

到了这里,关于单向循环链表的接口程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 数据结构入门(C语言版)线性表中链表介绍及无头单向非循环链表接口实现

    概念 : 线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素 。因此,为了表示每个数据元素与其直接后继数据元素之间的逻辑关系,对数据元素来说,除了存储其本身的信息之外,还需存储一个指示其直接后继的信息(即直接后继的存储位置)。这

    2023年04月09日
    浏览(38)
  • 单向循环链表(其一)

    单向循环的链表的使用规则和普通的单向链表没有较大的区别,需要注意: *单向循环链表的尾结点的指针域中必须指向链表的首结点的地址* ,由于带头结点的单向循环链表更加容易进行管理,如下图所示: 上图所示的就是一个典型的单向循环链表的结构,可以发现单向循

    2024年04月24日
    浏览(27)
  • 单向-->不带头-->非循环链表(简称:单链表)

    目录 一、链表的介绍 1.链表的概念 2.单链表的节点类型 3.单链表简图 二、单链表的增删查改 1.单链表的头插 2.单链表的尾插 3.单链表的头删 4.单链表的尾删 5.单链表pos位置之后插入一个节点 6.单链表删除pos位置后的一个节点         链表是一种物理存储结构上非连续、非顺

    2024年02月13日
    浏览(34)
  • 数据结构day06(单向循环链表、双向链表)

    双向链表的练习代码 head.h fun.c main.c 今日思维导图哈 ​​​​​​​

    2024年02月10日
    浏览(28)
  • 数据结构 模拟实现LinkedList单向不循环链表

    目录 一、链表的简单介绍 二、链表的接口 三、链表的方法实现 (1)display方法 (2)size得到单链表的长度方法 (3)addFirst头插方法 (4)addLast尾插方法 (5)addIndex指定位置插入方法 (6)contains方法 (7)remove删除第一个key值节点的方法 (8)removeAllKey删除所有值为key的方法

    2024年02月03日
    浏览(42)
  • 数据结构单向循环链表,创建以及增删改查的实现

    循环链表: 是另一种形式的链式存储结构。其特点是表中最后一个结点的指针域指向头节点,整个链表形成一个环。 单向循环链表的操作和单链表操作基本一致,差别在于:当链表遍历时,判别当前指针p是否指向表尾结点的终止条件不同。在单链表中,判别条件一般为p!=

    2024年02月16日
    浏览(34)
  • 指针穿梭,数据流转:探秘C语言实现单向不带头不循环链表

    本篇博客会讲解链表的最简单的一种结构:单向+不带头+不循环链表,并使用C语言实现。 链表是一种线性的数据结构,而本篇博客讲解的是链表中最简单的一种结构,它的一个结点的声明如下: 一个Node中包含2个部分:data用来存储数据,被称作“数据域”;next用来存储一个

    2024年02月05日
    浏览(38)
  • 【数据结构初阶】三、 线性表里的链表(无头+单向+非循环链表 -- C语言实现)

    ========================================================================= 相关代码gitee自取 : C语言学习日记: 加油努力 (gitee.com)  ========================================================================= 接上期 : 【数据结构初阶】二、 线性表里的顺序表(C语言实现顺序表)-CSDN博客  =========================

    2024年02月08日
    浏览(44)
  • [Collection与数据结构] 链表与LinkedList (一):链表概述与单向无头非循环链表实现

    上篇文章我们已经对顺序表进行了实现,并且对ArrayList进行了使用,我们知道ArrayList底层是使用数组实现的. 由于其底层是一段连续空间,当在ArrayList任意位置插入或者删除元素时, 就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低 ,因此ArrayList不适合做

    2024年04月26日
    浏览(42)
  • 单向不带头链表的使用

    后插(将值为x的元素插入到第i个位置)  前插操作     画图时应先画物理结构图在画逻辑结构图  链表的结点查找 结点的创建

    2024年01月20日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包