数据结构笔试题——基于C语言的链表功能函数实现
  t70Vbz9Rd99P 11天前 16 0

题目1

题目要求如下:

/**

* @function name: LList_CntdmFind

* @brief 查找链表中,倒数第k个位置上的节点

* @param :

​ @Head:链表头节点

​ @k :倒数第k个位置

* @retval :int 型返回值;返回-1时即为失败,返回0时表示成功;

* @date :2024/04/23

* @version 1.0

* @note

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

*/

int LList_CntdmFind(LList_t *Head, int k)
{
	LList_t *tmp1 = Head->next; // 用来遍历
	int length = 0;				// 用来存放元素个数
	int i = 0;
	if (NULL == Head) // 判断空表
		return -1;
	while (tmp1) // 计算元素个数
	{
		length++;
		tmp1 = tmp1->next;
	}
	if (length < k) // 如果k太大
	{
		printf("The k is invalid\n");
		return -1;
	}
	else if (length == k) // k正好是第一个
	{
		return Head->next->data;
	}
	else // 正常倒数
	{
		tmp1 = Head->next;
		for (i = 0; i < length - k; i++)
		{
			tmp1 = tmp1->next;
		}
		return tmp1->data;
	}
}

题目2

题目要求如下:

/**

* @function name: LList_DeleteMin

* @brief :删除单链表中的最小值节点

* @param :

​ @Head:链表头节点

* @retval :bool 型返回值;返回false时即为失败,返回true时表示成功;

* @date :2024/04/23

* @version 1.0

* @note

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

*/

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

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

暂无评论

推荐阅读
t70Vbz9Rd99P