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

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/

10-11 22:35
查看更多