why will it be a recursive destructor call i.e. why will all the nodes aftr the node to be deleted gets deleted?
About the destructor used in the class
First thing you need to know is that the destructor of a class object is invoked when the class object goes out of scope or is deleted using delete (if dynamically allocated )
let’s see the code :-
~Node(){
if(next!=NULL){
delete next;
}
}
here this is an example of recursive destructor
so if we have linked list as 1->2 ->3 ->4 ->5 -> NULL
and when we delete 1 then destructor is invoked for 1 and it checks for its next which is present and is the address for 2 thus invoking destructor of 2 similarly we reach 5 and check for its next which is NULL thus is returned to 4 and 5 is deleted and we return to 3 and 4 is deleted and finally we will be left with 1.