gpt4 book ai didi

c - 从链表中删除某些数字(递归)

转载 作者:行者123 更新时间:2023-11-30 17:00:17 25 4
gpt4 key购买 nike

大家好,我正在尝试创建一个删除特定数字的函数从链接列表中具有此编号的所有单元格中,但我的删除功能仅删除他到达末尾的第一个单元格,然后返回到主单元格这是我的代码:

struct num* deleteCell(struct num* point, int numdelete)
{
struct num* tempdelete = point;
if (point == NULL) // found the tail and dont found any match to delete
{
printf("not found\n");
return NULL;
}
else if (point->number == numdelete)// found one to delete
{
tempdelete = point->pNext;
free(point);
}
else// Just keep going
{
point->pNext = deleteCell(point->pNext, numdelete);
}
return tempdelete;
}

(point是指向第一个单元格的指针,numdelete是我要删除的数字)感谢各位帮忙!!!!

单独

最佳答案

struct num* deleteCell(struct num* point, int numdelete) {
if (point == NULL) {
return NULL;
}

if (point->number == numdelete) {
num* tempdelete = point->pNext;
free(point);
return deleteCell(tempdelete, numdelete);
}

point->pNext = deleteCell(point->pNext, numdelete);
return point;
}

关于c - 从链表中删除某些数字(递归),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37709566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com