小编Wom*_*bat的帖子

如何在C++中正确删除链表的节点

我觉得好像我实际上没有删除节点并释放内存.我想我只是在移动指针,所以当我打印链表时,列表不打印我删除的元素.所以我的问题是我实际上是删除节点还是我只是简单地重新排列指针所以它看起来像我正在删除节点(基本上只是破坏链接但不删除节点)?感谢您的任何帮助.

void SLL::deleteNode(int target){
Node *current = new Node;
Node *previous = new Node;

for (current = front->next, previous = front; current != NULL; current = current->next, previous=previous->next){
    if (previous->data == target && previous == front){
        front = previous->next;
        delete[] previous;
        return;
    //This if statement deletes the element if its the front
    }

    else {

        if (previous->data == target && previous->next == NULL){
            previous = NULL;
            delete[] current;
            return;
        //This if statement deletes the node if it is the back …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list nodes

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

linked-list ×1

nodes ×1