Implementation of class node for hashtable

when we are calling the node destructor why it is recursive call

Hello @shashankmaheshwari054
read this:
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 ),
Now let’s say we have a linked list {1->2->3->4->5} at some index of hash table ,say .hash[idx] points to the head of that linked list i.e.1 now when hash table goes out of scope then the constructor will be invoked for hash[idx] which is node 1, now if node 1 is deleted only then we will have 2->3->4->5 still remaining in the memory with no pointer leading to them, so we try to delete the entire linked list recursively ,
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.

but how it is calling destuctor of other nodes

@shashankmaheshwari054 destructor is called automatically as the object goes out of scope and as nodes are linked thats why the destructor for the other nodes are called with that:
take this example:
Suppose,
oldbuckets[i] refers to or stores the address of the head node of the linked list: 2->4->8 i.e. the address of 2.

So, when the compiler executes the statement:
delete oldbuckets[i];
which means delete the node having value 2 i.e. the head of the linked list.

But, i have explained in my previous reply: delete automatically propagates forward in the linked list.

  1. before calling the destructor for the node with value 2, it iterates to the next of it i.e. node with value 4.
  2. thereafter, before calling the destructor for the node with value 4, it iterates to the next of it i.e. node with value 8.
  3. As the next of 8 is NULL. So, it first deletes the node with value 8(destructor)
  4. Then comes back to the previous node and deletes the node with value 4
  5. At last, a node with head.

This is all automatically handled by the compiler.

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.