[Solved] Segmentation fault (core dumped) unable to find the bug


Node* temp = head;
while(temp!=NULL){
    temp = temp->next;
}

This will guarantee that the only way you get out of this loop is if temp is null. And what do you do on the next line?

temp->next = newNode;// I think fault is here.

Dereference temp! You probably wanted temp to point to the last node after the loop. You could do that with:

Node* temp = head;
while(temp!=NULL && temp->next != NULL){
    temp = temp->next;
}

Or even cleaner (imo) since you’ve already checked head to be null in the if branch above this, we don’t need to check temp != NULL and we can condense it all into a for loop:

for(Note *temp = head; temp->next != NULL; temp = temp->next) { }

2

solved Segmentation fault (core dumped) unable to find the bug