How do I properly delete nodes of linked list in C++

WombatCombat

I feel as if I am not actually deleting the node and freeing up memory. I think I am just moving pointers around so when I print the linked list the list doesn't print the element I deleted. So my question is am I actually deleting the node or am I just simply rearranging the pointers so it looks like I am deleting the nodes(Essentially just breaking the links but not deleting the node)? Thank you for any help.

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
        }


        else if (current->data==target)
        {
            previous->next = current->next;
            delete[] current;
            return;
        //This if statement deletes a node if it is in the middle
        }
    }
    }

    delete[] current;
    delete[] previous;
}
Alexey Voytenko
Node *current  = new Node;
Node *previous = new Node;

This code causes memory leaks - you are never deleting this memory. You can declare pointers without memory allocation:

Node *current  = nullptr;
Node *previous = nullptr;

delete will delete the memory of the pointer so you will actually delete Nodes. But using delete[] for the Node* is incorrect, it should be used only for arrays - the memory allocated with new[]. Improper use leads to undefined behaviour. So, to properly delete nodes delete them with operator delete.

Use memory leaks detection tools to know are there memory leaks in you program.

The code to delete a list element: say, we have pHead which points to the head of the list (but it would give you much more if you write such things yourself):

Node* pCur  = pHead;
Node* pPrev = pCur;

while (pCur && pCur->data != target) {
    pPrev = pCur;
    pCur  = pCur->next;
}

if (pCur==nullptr)  // not found
   return NOT_FOUND;

if (pCur == pHead) {   // first element matches
    pHead = pCur->next;
} else {
    pPrev->next = pCur->next;
}

// pCur now is excluded from the list

delete pCur;     // deallocate its memory

Alternative Using Pointer To Pointer (Community Addition)

The above can take on new light when you use the actual pointers in the list to perform the enumeration. The following starts with pp being assigned the address of the head pointer (not the node it points to; the actual pointer itself). We walk the list until pp hold the address of a pointer that is pointing to a node with the target to delete (could be the head pointer, could be a next pointer in some node, makes no difference). The pointer being addressed is set to its own node's next value, then the target node is removed.

This really should be watched in a debugger to see how it works, but the algorithm is remarkably simple given what is really going on:

Node **pp = &pHead;
while (*pp && (*pp)->data != target)
    pp = &(*pp)->next;

if (*pp)
{
    Node *victim = *pp;
    *pp = victim->next;
    delete victim;
}

Thats all of it. And you get head-node removal without having to special case it for free. Hope this helps as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to "completely" delete all nodes in a linked list?

From Dev

How do I properly deallocate memory allocated with a linked list?

From Dev

How do I delete the last element added into a linked-list in C++

From Dev

How to delete the list when linked list nodes are constructed with "new"?

From Dev

How do I sort a linked list in a alphabetical order in c

From Dev

How to delete a node in a linked list in c

From Dev

Linked list delete in C

From Dev

C Linked List Nodes Not Storing

From Dev

How do I properly delete ACAD DBObjects?

From Dev

How to compare linked list nodes?

From Dev

How do I iterate over my linked list to search for and delete specific strings?

From Dev

How do I delete a node at the end of a linked list without relying on methods?

From Dev

C - How to Iterate through sub-nodes in a linked list?

From Dev

How to add together the values in the nodes of my linked list, C++

From Dev

How to implement a linked-list with multiple nodes in C?

From Dev

How to test if linked list nodes were deleted in C++

From Dev

How do I reverse through a linked list

From Dev

How do I use this Linked List implementation?

From Dev

How do I Sort the names in a Linked List?

From Dev

C - Linked List - Delete Node

From Dev

C delete node in a linked list

From Dev

C Delete Linked List By Reference?

From Dev

How do I update this old C++ doubly linked list code to C++11?

From Dev

Remove nodes of a linked list on condition (C)

From Dev

C++: incorrect swapping of nodes in linked list

From Dev

Swapping nodes in double linked list in C

From Dev

How to delete the first node of a linked list in c++

From Dev

How to delete first node from linked list? (C)

From Dev

How to implement linked list with 1 million nodes?

Related Related

  1. 1

    How to "completely" delete all nodes in a linked list?

  2. 2

    How do I properly deallocate memory allocated with a linked list?

  3. 3

    How do I delete the last element added into a linked-list in C++

  4. 4

    How to delete the list when linked list nodes are constructed with "new"?

  5. 5

    How do I sort a linked list in a alphabetical order in c

  6. 6

    How to delete a node in a linked list in c

  7. 7

    Linked list delete in C

  8. 8

    C Linked List Nodes Not Storing

  9. 9

    How do I properly delete ACAD DBObjects?

  10. 10

    How to compare linked list nodes?

  11. 11

    How do I iterate over my linked list to search for and delete specific strings?

  12. 12

    How do I delete a node at the end of a linked list without relying on methods?

  13. 13

    C - How to Iterate through sub-nodes in a linked list?

  14. 14

    How to add together the values in the nodes of my linked list, C++

  15. 15

    How to implement a linked-list with multiple nodes in C?

  16. 16

    How to test if linked list nodes were deleted in C++

  17. 17

    How do I reverse through a linked list

  18. 18

    How do I use this Linked List implementation?

  19. 19

    How do I Sort the names in a Linked List?

  20. 20

    C - Linked List - Delete Node

  21. 21

    C delete node in a linked list

  22. 22

    C Delete Linked List By Reference?

  23. 23

    How do I update this old C++ doubly linked list code to C++11?

  24. 24

    Remove nodes of a linked list on condition (C)

  25. 25

    C++: incorrect swapping of nodes in linked list

  26. 26

    Swapping nodes in double linked list in C

  27. 27

    How to delete the first node of a linked list in c++

  28. 28

    How to delete first node from linked list? (C)

  29. 29

    How to implement linked list with 1 million nodes?

HotTag

Archive