Linked list operator overloading

why is the code showing segmentation fault

there are few mistakes in your code

  1. you have to initialize head=NULL in main();

  2. in insertattail() you also have to consider the case when head==NULL

    if(head==NULL){
       head=new node(d);
       return;
    }
    
  3. in insertattail function as you are passing head as reference variable hence you have to change the head only when head =NULL otherwise you have to use temp
    like this

 node*temp=head;
    while(temp->next!=NULL){
        temp=temp->next;
    }
    node *n=new node(d);
    temp->next=n;
    n->next=NULL;

i have done all changes in modified Code

Modified Code

i hope this helps
if you want to ask something about this feel free to ask
if yes show your response with :heart: and don’t forgot to mark doubt as resolved

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.