i didnot understand the explaination of destructor
I didnot understand the explaination of destructor
hi @Aditya-Kushwaha-914550392281281, 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.
try to dry run it yourself
In case of any doubt feel free to ask
mark your doubt as resolved If you got the answer
node is already a user defined datatype why is the need off node in this we are using a one morre datatype