C++ 为什么我不能删除带有引用的节点

C++ 为什么我不能删除带有引用的节点,c++,C++,为什么它不起作用,双打印功能总是打印abac您删除l4,但您从不更改l3,它指向仍然包含数据的l4内存(现在已删除)('c') 你需要 delete pending; pending=NULL; 从列表中删除元素(当然,您仍然必须将其删除) 要使用deletelement函数,需要将其更改为遍历列表(伪代码): 我真的很喜欢你的标题。发生了什么让你相信你不能?你应该试着解释你的实际问题是什么,是编译问题还是运行时问题?正如克里斯所说,我改变了它,你能再次看到它吗! delete pending

为什么它不起作用,双打印功能总是打印abac

删除l4
,但您从不更改
l3
,它指向仍然包含数据的
l4
内存(现在已删除)('c')

你需要

delete pending; pending=NULL;
从列表中删除元素(当然,您仍然必须将其删除)

要使用
deletelement
函数,需要将其更改为遍历列表(伪代码):


我真的很喜欢你的标题。发生了什么让你相信你不能?你应该试着解释你的实际问题是什么,是编译问题还是运行时问题?正如克里斯所说,我改变了它,你能再次看到它吗!
delete pending; pending=NULL;
l3->setNext(NULL);
void deleteElement( Element head , Element toBeDeleted)
{
    //are we deleting head (the first element of the list?) 
    //yes then head should be nulled, and delete as normal

    current = head ; ancestor = head;

    //scan through list (current becomes current->next until no more)
    //until we find toBeDeleted
    //maintain ancestor as we go 

    //if found set ancestor->next to current->next
    //delete toBeDeleted
}