I didnt understand insertAtHead() function. We are passing nodehead = Null to insertAtHead() in first case there if condition is true then it executes code within it and calls constructor of node class ie, node(int d) here the next is already initialised to NULL. So, the next time when we are calling insertAtHead() from main the address ie nodenext should be NULL and should execute the if condition , so why its not happening so?
Insertion Linked List
Hello @dhanshree_p18 where we are doing in the if coindition like if head ==NULL
in that if it is the case then we are doing like head=new node(d) which means that we have to create the new node in which there will be one next for pointing to the next node but currently it is pointing to NULL.
and in the data of that node we have intialsed it with the value which you are sending. i.e ‘d’ .
if you have any doubt you can ask here:
HappyLearning!!
So the next time if we call again the insertAtHead() then it should statisfy the if if condition again na? because address stored in previous node is NULL na?
no then the head of our linked list is not at null and our head contains one node.
that why after adding first node whnever we will go into insert at head then it will never encounter the if condition.
then the nodes will be add afterwards.
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.