单链表功能函数练习——按规定插入指定节点及删除最小值节点(C语言)
  t70Vbz9Rd99P 11天前 14 0

本文属于数据结构中,单链表练习的一部分。共设计了两个功能函数,详细信息如下。

设计在指定值的位置处,插入指定数据的函数

* 函数名称: LList_DestInsert

* 函数功能: 在指定值位置处,插入指定的数据data

* 函数参数:

​ LList_t *Head: 需要操作的链表头节点

​ DataType_t dest: 插入位置的值

​ DataType_t data: 需要插入的指定的数据

* 返回结果: true or false

* 注意事项: None

* 函数作者: ni456xinmie@163.com

* 创建日期: 2024/04/22

* 修改历史:

* 函数版本: V1.0

bool LList_DestInsert(LList_t *Head, DataType_t dest, DataType_t data)
{
	// 1.创建新的结点,并对新结点进行初始化
	LList_t *New = LList_NewNode(data);
	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}
	LList_t *tmp = Head;
	while (tmp->data != dest && tmp->next != NULL) // 2.移动到指定位置节点
	{
		tmp = tmp->next;
	}
	if (NULL == tmp->next)
	{
		if (tmp->data == dest)
		{
			New->next = NULL; // 3.如果指定目标值在末尾,且dest正好也在末尾,可进行尾插操作
			tmp->next = New->next;
			return true;
		}
		else
		{
			printf("There is no dest\n"); // 4.如果未找到指定目标值,则返回
			return false;
		}
	}
	New->next = tmp->next; // 5.如果指定目标值在中间,则进行插入操作。
	tmp->next = New->next;
	return true;
}

设计删除单链表钟最小值节点的函数

* 函数名称: LList_DeleteMin

* 函数功能: 删除单链表中的最小值节点

* 函数参数:

* LList_t *Head: 需要操作的链表头节点

* 返回结果: true or false

* 注意事项: None

* 函数作者: ni456xinmie@163.com

* 创建日期: 2024/04/22

* 修改历史:

* 函数版本: V1.0

···

bool LList_DeleteMin(LList_t *Head)
{
	LList_t *tmp1 = Head->next;		// 用来遍历
	LList_t *tmpFormer = Head;		// 用来存放目标指针前一个节点
	LList_t *tmpDest = Head->next;	// 用来存放目标指针
	DataType_t tmpMin = tmp1->data; // 用来存放最小值
	if (!tmp1)						// 如果是空表
	{
		printf("The list is NULL");
		return false;
	}
	if (!tmp1->next) // 只有一个元素,就删掉
	{
		free(Head->next);
		Head = NULL;
		return true;
	}
	while (tmp1->next) // 两个以上的元素,定位到最小元素
	{
		if (tmpMin > tmp1->next->data)
		{
			tmpMin = tmp1->next->data;
			tmpDest = tmp1->next;
			tmpFormer = tmp1;
		}
		tmp1 = tmp1->next;
	}
	tmpFormer->next = tmpDest->next; // 进行删除操作
	free(tmpDest);
	return true;
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

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

暂无评论

推荐阅读
t70Vbz9Rd99P