单向循环链表接口设计(C语言)

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

单向循环链表接口设计

/**

* @file name: 单向循环链表接口设计

* @brief :设计单向循环链表,实现各种功能函数并测试

* @author ni456xinmie@163.com

* @date 2024/04/23

* @version 1.0

* @property

* @note

* CopyRight (c) 2023-2024 ni456xinmie@163.com All Right Reseverd

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


构造单向循环链表结构体

// 指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;

// 构造单向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{
	DataType_t data;				 // 结点的数据域
	struct CircularLinkedList *next; // 结点的指针域

} CircLList_t;

创建一个空单向循环链表并初始化

CircLList_t *CircLList_Create()
{
	// 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;
	return Head; // 3.把头结点的地址返回即可
}

创建新的结点,并对新结点进行初始化

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;
}

功能函数:从首节点进行插入元素

bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{
	CircLList_t *new = CircLList_NewNode(data);
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // empty list
	{
		Head->next = new;
		new->next = new;
		return true;
	}
	while (tmp->next != Head->next) // normal situation,find the last node
		tmp = tmp->next;
	new->next = Head->next;
	Head->next = new;
	tmp->next = new;
	return true;
}


功能函数:从尾部插入新元素

bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{
	CircLList_t *new = CircLList_NewNode(data);
	if (Head->next == Head) // judge is the null
	{
		Head->next = new;
		new->next = new;
		return true;
	}
	CircLList_t *tmp;
	while (tmp->next != Head->next) // when the normal situation,find the last node
		tmp = tmp->next;
	tmp->next = new;
	new->next = Head->next;
	return true;
}


功能函数:从指定位置插入新元素

bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{
	CircLList_t *tmp = Head->next;
	DataType_t i = Head->data;
	if (Head->next == Head) // judge the empty list
	{
		printf("The list is empty");
		return false;
	}
	CircLList_t *new = CircLList_NewNode(data);
	while (destval != tmp->data && tmp->next != Head->next)
	{
		tmp = tmp->next;
	}
	if (destval == tmp->data)
	{
		new->next = tmp->next;
		tmp->next = new;
		return true;
	}
	else
	{
		printf("There is no destval\n");
		return false;
	}
}

功能函数:遍历打印链表

bool CircLList_Print(CircLList_t *Head)
{
	// 对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead = Head;

	// 判断当前链表是否为空,为空则直接退出
	if (Head->next == Head)
	{
		printf("current linkeflist is empty!\n");
		return false;
	}

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

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

		// 判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == Head->next)
		{
			break;
		}
	}
	return true;
}


功能函数:删除首节点

bool CircLList_HeadDel(CircLList_t *Head)
{
	// 对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead = Head->next;
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // 判断当前链表是否为空,为空则直接退出
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	while (tmp->next != Head->next) // find the last node
		tmp = tmp->next;
	tmp->next = Phead->next;
	Head->next = Phead->next;
	Phead->next = NULL;
	free(Phead);
	return true;
}

功能函数:删除尾部节点

bool CircLList_TailDel(CircLList_t *Head)
{

	CircLList_t *tmpFormer;
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // 判断当前链表是否为空,为空则直接退出
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	while (tmp->next != Head->next) // find the last node
	{
		tmpFormer = tmp;
		tmp = tmp->next;
	}
	tmpFormer->next = Head->next;
	tmp->next = NULL;
	free(tmp);
	return true;
}

功能函数:删除指定位置的节点

bool CircLList_MidDel(CircLList_t *Head, DataType_t destval)
{
	CircLList_t *tmpFormer;
	CircLList_t *tmp = Head->next;
	if (Head->next == Head) // 判断当前链表是否为空,为空则直接退出
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	while (tmp->data != destval && tmp->next != Head->next) // find the specific node
	{
		tmpFormer = tmp;
		tmp = tmp->next;
	}
	if (tmp->data == destval)
	{
		tmpFormer->next = tmp->next;
		tmp->next = NULL;
		free(tmp);
		return true;
	}
	else
	{
		printf("The is no destival\n");
		return false;
	}
}


主函数,调用并测试各功能函数

int main()
{
	CircLList_t *H = CircLList_Create();
	CircLList_HeadInsert(H, 10);
	CircLList_HeadInsert(H, 20);
	CircLList_HeadInsert(H, 30);
	CircLList_TailInsert(H, 30);
	CircLList_TailInsert(H, 40);
	CircLList_DestInsert(H, 30, 15);
	CircLList_Print(H);
	puts("");
	CircLList_HeadDel(H);
	CircLList_Print(H);
	puts("");
	CircLList_TailDel(H);
	CircLList_Print(H);
	puts("");
	CircLList_MidDel(H, 12);
	CircLList_Print(H);
	return 0;
}

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

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

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

相关文章

  • 【数据结构初阶】三、 线性表里的链表(无头+单向+非循环链表 -- C语言实现)

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

    2024年02月08日
    浏览(44)
  • 数据结构_链表_单向循环链表的初始化、插入、删除、修改、查询打印(基于C语言实现)

    版本: 2024年4月25日 V1.0 发布于博客园 目录 目录 单向循环链表公式 初始化单向循环链表 构建单向循环链表结点 创建一个空链表(仅头结点) 创建一个新结点 插入数据 头插 中插 尾插 删除数据 头删 中删 尾删 查询打印数据 遍历打印 测试 测试结果: 完整代码 CircularLinkedLis

    2024年04月25日
    浏览(40)
  • 单向循环链表(其一)

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

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

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

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

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

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

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

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

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

    2024年02月16日
    浏览(34)
  • 数据结构入门(C语言版)线性表带头双向循环链表接口实现

    在上一篇博客我们讲述了链表的概念和结构,还实现了无头单向非循环链表接口写法,那么这一章节,我们来实现另一种常用的链表组成结构——带头双向循环链表。 如果对前面的链表基本概念还是不了解,可以看作者的上一篇博客: 线性表中链表介绍及无头单向非循环链

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

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

    2024年04月26日
    浏览(42)
  • 双向循环链表的接口

    在链表的头部插入 在链表的尾部插入 在链表指定数据节点后插入 删除链表头部的节点 删除链表尾部的节点 删除链表指定数据节点后的节点

    2024年04月25日
    浏览(21)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包