0
votes

I have a linked list contains 3 nodes like the image shown: enter image description here

There is a head pointer and temp1 pointer point to the front of the list, and tail point points at the end of the list.

I want to remove all the nodes, and change it back to its original initial form ( tail = NULL, head = first_node , but the first node doesn't have any value in the data and next field).

Because I want to start putting up some new values in it. To remove all those data, is this code going to remove nodes inside this linked list and left with the first node with no values in data and next field?

This code is in C++:

while(temp1!=tail)
{
    temp1 = temp1->next;
    if(temp1->next == tail)
    {
        tail=temp1;
        temp1 = temp1->next;
        free(temp1);    
    }
}

But then, does this mean only the last node will be deleted? are there any way to delete all the nodes except the first one?

4
You should consider using the STL <list> unless you have a good reason to roll your own. <list> supports clearing of all elements or a range. See cplusplus.com/reference/stl/listEdChum
@Abyx: The sample code he posted is actually valid C and C++ and the question is tagged C++.ereOn

4 Answers

5
votes

To delete all nodes except the first node, you can try below code.

temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{   
    head->next = temp1->next;
    temp1->next = NULL;
    free(temp1);
    temp1 = head->next;
}

This will delete all nodes except first one. But the data with the first node will remain as it is.

5
votes

Disclaimer: I assume it's only for learning purposes and in real-world scenario you would use std::list<> or similar container.

For single-linked list, you can just drop all this burden an let the stdlib manage the pointers:

class Node {
    std::unique_ptr<Node> next;
};

You can safely use .reset() method to make operations on the list:

Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:

  • Saves a copy of the current pointer old_ptr = current_ptr
  • Overwrites the current pointer with the argument current_ptr = ptr
  • If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).

From http://en.cppreference.com/w/cpp/memory/unique_ptr/reset.

And that's pretty much what you would do when deleting. I believe you can also use unique_ptr::swap(), to easily manipulate your nodes.

0
votes

Instead of free, C++ uses delete function.

Check the link to have deep knowledge about all kind of operations(including recursive or iterative delete) on linked lists.

-1
votes
temp1 = head->next;

while(temp1!=NULL) // as I am considering tail->next = NULL
{
    head->next = temp1->next;
    temp1->next = NULL;
    free(temp1);
    temp1 = head->next;
}

The logic for this would be more correct if it is this way.

After the statement

free(temp1);

Add the condition

if (head -> next != NULL)
    temp1 = head->next;

Since after deleting the last node there is no point in reassigning the address of head pointer to temp1.