how does the delete keyword invoke the destructor?
About the delete keyword
@Senjuti256, destructor is evoked whenever the object goes out of scope or memory is freed (memory is deallocated), which is done by delete keyword. this is how destructor is defined on the contrary to constructor
thn how is it recursively getting called to delete the other nodes of that index of the hash table?
@Senjuti256, we have applied recursive destructor ,which deletes the nodes until it reaches NULL ,let me explain how …
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 for better understanding