In defining destructor

why while is not used we are iterating till end, and how if recursively able to delete all the nodes

hello @great

this destructor is responsible for recursive deletion.

   ~node(){
        if(next!=NULL){
            delete next;
        }
    }

It is said to be recursive in nature because a node is deleted, its destructor gets invoked. As the destructor is invoked, the above code executes and it checks whether the current node has a next node. If the next node exists, it says to delete that next node.
Since the next node is also an instance of this very same class, the same destructor executes for it, this time for the next node. The entire process repeates and will continue to call the destructors for node after node until it reaches a node that does not have next . In that case, the if statement will simply not run and thus we will simply return from there.
Hence this destructor runs in a recursive manner.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I have one doubt, why destructor is getting invoked everytime we delete the node

its c++ langauge functionality whenerve we delete object the destructor will get called automatically

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.