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

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

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

* 函数名称: 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;
}
posted @   Rice_rice  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示