从链接列表中删除节点(递归) [英] Delete node from linked list (recursively)

查看:108
本文介绍了从链接列表中删除节点(递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

!=我目前正在使用下面的擦除递归bool函数,它将list和int作为参数,如果int被发现和删除则返回true,如果在列表中找不到则返回false。 似乎工作,但 是删除列表中的 下一个int号 不是当前 :

!=I am currently working on the following erase recursive bool function that thakes list and int as arguments and return true if the int was found and deleted and false if it was not found in the list. It seems to work, but the problem is that it deletes the next int number in the list, and not the current:

 typedef struct E_Type * List;

 struct E_Type
 {
        int data;
        List next = 0;
 };







bool erase(const List & l, int data){
List current = l;   
if (current == 0)
{
   return false;
 }
else if (current->data == data)
{
      List deleteNode = new E_Type;
     deleteNode = current->next;//probably this causes the error, but how can I point it to the current without crashing the program
     current->next = deleteNode->next;
     delete deleteNode;
     return true;
}

else if (current->data != data)
{
      return erase(current->next, data);
}


}


推荐答案

您正在考虑的唯一节点是当前节点,因此您必须具有修改 l 的规定:

The only node you're considering is the current one, so you must have a provision for modifying l:

if (current->data == data)
{
  l = current->next;
  delete current;
  return true;
}

这篇关于从链接列表中删除节点(递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆