本文屬于數(shù)據(jù)結(jié)構(gòu)中,單鏈表練習(xí)的一部分。共設(shè)計(jì)了兩個(gè)功能函數(shù),詳細(xì)信息如下。
設(shè)計(jì)在指定值的位置處,插入指定數(shù)據(jù)的函數(shù)
* 函數(shù)名稱: LList_DestInsert
* 函數(shù)功能: 在指定值位置處,插入指定的數(shù)據(jù)data
* 函數(shù)參數(shù):
? LList_t *Head: 需要操作的鏈表頭節(jié)點(diǎn)
? DataType_t dest: 插入位置的值
? DataType_t data: 需要插入的指定的數(shù)據(jù)
* 返回結(jié)果: true or false
* 注意事項(xiàng): None
* 函數(shù)作者: ni456xinmie@163.com
* 創(chuàng)建日期: 2024/04/22
* 修改歷史:
* 函數(shù)版本: V1.0文章來源:http://www.zghlxwxcb.cn/news/detail-855854.html
bool LList_DestInsert(LList_t *Head, DataType_t dest, DataType_t data)
{
// 1.創(chuàng)建新的結(jié)點(diǎn),并對(duì)新結(jié)點(diǎn)進(jìn)行初始化
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.移動(dòng)到指定位置節(jié)點(diǎn)
{
tmp = tmp->next;
}
if (NULL == tmp->next)
{
if (tmp->data == dest)
{
New->next = NULL; // 3.如果指定目標(biāo)值在末尾,且dest正好也在末尾,可進(jìn)行尾插操作
tmp->next = New->next;
return true;
}
else
{
printf("There is no dest\n"); // 4.如果未找到指定目標(biāo)值,則返回
return false;
}
}
New->next = tmp->next; // 5.如果指定目標(biāo)值在中間,則進(jìn)行插入操作。
tmp->next = New->next;
return true;
}
設(shè)計(jì)刪除單鏈表鐘最小值節(jié)點(diǎn)的函數(shù)
* 函數(shù)名稱: LList_DeleteMin
* 函數(shù)功能: 刪除單鏈表中的最小值節(jié)點(diǎn)
* 函數(shù)參數(shù):
* LList_t *Head: 需要操作的鏈表頭節(jié)點(diǎn)
* 返回結(jié)果: true or false
* 注意事項(xiàng): None
* 函數(shù)作者: ni456xinmie@163.com
* 創(chuàng)建日期: 2024/04/22
* 修改歷史:
* 函數(shù)版本: V1.0
···文章來源地址http://www.zghlxwxcb.cn/news/detail-855854.html
bool LList_DeleteMin(LList_t *Head)
{
LList_t *tmp1 = Head->next; // 用來遍歷
LList_t *tmpFormer = Head; // 用來存放目標(biāo)指針前一個(gè)節(jié)點(diǎn)
LList_t *tmpDest = Head->next; // 用來存放目標(biāo)指針
DataType_t tmpMin = tmp1->data; // 用來存放最小值
if (!tmp1) // 如果是空表
{
printf("The list is NULL");
return false;
}
if (!tmp1->next) // 只有一個(gè)元素,就刪掉
{
free(Head->next);
Head = NULL;
return true;
}
while (tmp1->next) // 兩個(gè)以上的元素,定位到最小元素
{
if (tmpMin > tmp1->next->data)
{
tmpMin = tmp1->next->data;
tmpDest = tmp1->next;
tmpFormer = tmp1;
}
tmp1 = tmp1->next;
}
tmpFormer->next = tmpDest->next; // 進(jìn)行刪除操作
free(tmpDest);
return true;
}
到了這里,關(guān)于單鏈表功能函數(shù)練習(xí)——按規(guī)定插入指定節(jié)點(diǎn)及刪除最小值節(jié)點(diǎn)(C語言)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!