Insert at Tail Linked list

In the while loop if I write while(tail!=NULL) instead of while(tail->next!=NULL), it is showing segementation fault. Why is it happening ?

Let’s assume the linked list is 1->2->3->NULL.
If we go by while(tail!=NULL) the loop will stop only when tail is pointing to the NULL. If we add a new Node by tail->next = new Node(d) then it will not be connected to the linkedlist, since NULL is not a part of the linkedlist. So, we have to use while(tail->next!=NULL) as the condition of the while loop so that the tail pointer stops at the last node and we can add a new node to the linkedlist.