Delete linked list

delete node linked list c++
delete a node from linked list algorithm
delete node at given position in a linked list
remove element from linked list c++
delete a specific node in linked list java
delete first node in linked list c++
delete node at given position in a linked list in c
delete a doubly linked list

Hey I wonder about I have written a C++ linked list, where I call a destructor to go through an allocated linked list and delete every node found. However what I found is that although it goes through the linked list and delete every occurance it will still print out values. Although just some scrap values.

But shouldn't it be when I delete the linked_list it shouldn't be printable next time? I'm create a linked list by using the new, and delete when I remove the list

sorted_list::~sorted_list()
{
    // Destructor implementation
    destroy(this->first);
    cout << "Destructor called sorted_list" << endl;
}

void sorted_list::destroy(list_link* item)
{
  if (item)
  {
    destroy(item->next);
    delete item;
  }
}
print function
void sorted_list::print() {

    if(this->first)
    {
        iteratorn *traverse = new iteratorn(this->first);
        while( !traverse->iterator_end() )
        {
            cout << traverse->iterator_get_key() << " ";
            traverse->iterator_next();
        }
        delete traverse;
    }
    else
        cout << "list empty" << endl;
}

When you access a destructed object, the behaviour is undefined. In fact, deleting an object doesn't clear the memory, just marks it available, so if you execute some operations on already deleted object, they may do something reasonable. But again, the object is destructed, so you must not access it.

Of course, you should not retain any pointers to the object belonging to the linked list after it's destructed, because those objects will be destructed as well.

By the way, your sorted_list::destroy is recursive, which is quite inefficient. You would need perhaps to replace it with iterative approach:

void sorted_list::destroy(list_link* item)
{
    while (item)
    {
        list_link* old = item;
        item = item->next;
        delete old;
    }
}

(And you should take into account @Roger Pate's comment and not delete this->first the second time after calling destroy(this->first);.)

Linked List, Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in linked list. To delete a node from  Delete Linked List: Write a function that takes a linked list, deallocates all of its memory and sets its head pointer to nullptr (the empty list) The idea is to iterate through the list and delete each node encountered. There is a slight complication inside the loop, since we need extract the .next pointer before

When declaring the link, which probably looks more or less like this:

struct list_link {
    int data;
    list_link *next;
};

You could slip a destructor in:

struct list_link {
    int data;
    list_link *next;
    ~list_link() { delete next; }  // MAKE SURE NULL-TERMINATED LIST
};

This way if you want to delete the list you can simply:

delete first;

MAGIC!!

Delete a Linked List in C, Delete Linked List: Write a function that takes a linked list, deallocates all of its memory and sets its head pointer to nullptr (the empty list) The idea is to iterate​  Here are some constraints for our example: The list holds char 's. Deletion can occur anywhere in the list. The delete operation will not return the element that we delete, it only need remove it from the list. Now, recall the basic structure of a singly-linked list: list | v --------- --------- --------- |

Missing part is nullifying. After deleting, you must nullify the first node at least. I would nullify every node after delete.

Delete linked list, When you access a destructed object, the behaviour is undefined. In fact, deleting an object doesn't clear the memory, just marks it available,  Hey I wonder about I have written a C++ linked list, where I call a destructor to go through an allocated linked list and delete every node found. However what I found is that although it goes through the linked list and delete every occurance it will still print out values. Although just some scrap values.

Delete a linked list (all nodes), Delete all nodes in a linked list. Free() function is used to deallocate the memory.Duration: 5:36 Posted: Mar 18, 2018 Delete a Linked List node at a given position. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution. If node to be deleted is root, simply delete it. To C/C++. filter_none. edit. close. play_arrow. link. brightness_4. code. // A complete working C program to

Write a function to delete a Linked List, Explanation for the article: http://www.geeksforgeeks.org/write-a-function-to-​delete-a Duration: 6:08 Posted: Nov 5, 2016 Linked List | Set 3 (Deleting a node) 1) Find previous node of the node to be deleted. 2) Change the next of previous node. 3) Free memory for the node to be deleted.

Deleting from a Linked List, There are a few steps to deleting a specific element from the list: Find the node with the element (if it exists). Remove that node. Reconnect the linked list. Update the link to the beginning (if necessary). Second, keep track of the previous node while traversing the linked list forward. The node to be deleted can be anywhere in the linked list, there are three possibilities: Node to be deleted is the head node of linked list. Node to be deleted is any of the middle nodes. Node to be deleted is last node of linked list.

Comments
  • Not enough code. Show the code that builds the list, and show the code that prints out values that it shouldn't.
  • You delete this->first twice (once in destroy).
  • You don't need to assign NULL (or even 0) to this->first in the destructor. The list is released, it's gone, utterly inaccessible.
  • @Roger Pate @wilhelmtell thanks for input I remove the double delete of this->first now
  • Yeah, always destroy lists iteratively, because the recursive method might run out of stack space if the list is too large.
  • @Vlad @Timo this is not accurate. Any modern C++ compiler should know how to optimize away at least tail-recursions. Here it is actually straight-forward to convert the recursion to a tail-recursion, and the result object-code should be identical to that of a loop: if( ! item ) return; list_link* next = item->next; delete item; destroy(next);
  • @wilhelmtell: it's a dangerous practise to assume that the optimizer will optimize the recursion out for you. Unless such an optimization is guaranteed by C++ standard, I would not rely on it.
  • I've been trying around, and still can't get why I still retain scrap values. I actually want the program to crash when I try to access an already deleted linked list, but instead it will print out some scrap values. I have added in my print function also.
  • @starcom: you cannot get a guaranteed crash, the behaviour is just undefined. Look, when you destruct the object, the memory gets freed, and returned to the global memory pool. So it may be overwritten with any values at any time, or it may as well still have its old values, you never know. If it happens to have the old values, your print function may not crash. And you cannot guarantee that the function will crash, because this needs the memory to be filled with some special values--but that is not possible, because the memory can be reused by other objects. You see?