The head of the list only needs to be set if the list is presently empty. Your add_student() function always resets the head to the last node in the list during the else clause. Just cut that line out and all will be well:
if (head==NULL) {
temp->next=head;
head=temp;
} else {
struct student *temp1=new struct student;
temp1=head;
while (temp1->next!=NULL) {
temp1=temp1->next;
}
temp->next=NULL;
temp1->next=temp;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401