You are viewing a single comment's thread. Return to all comments →
//in cpp , giving a segmentation fault
why is the above code giving me segmentation fault
When you exit the while loop you have a pointer pointing to nowhere. It didn't matter on the Print exercise, but here it does.
In your else statement, create another Node* pointer variable (*tail, for instance), and make it receive your *p at each iteration.
At the last iteration, it will be pointing to the last Node structure, which will be the tail of the linked list. Then your p->next=temp; will work :-) !