!=我目前正在研究以下擦除递归布尔函数,它将 list 和 int 作为参数,如果找到并删除了 int,则返回 true,如果在列表中找不到它,则返回 false。它似乎有效,但问题是它删除了列表中的下一个 int 数字,而不是当前的:
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);
}
}