Ok, well, I got it partially working now. It will add on to the top of the list and to the end of the list, but will not add any names in between. So i can add say, John, then I can add Jim, then add Stuart. But If I try to add Kim, it just doesn't show up. Here's my code now:
Code:
void compclass::addstudent(char tname[]) {
student *newstud = new student;
student *head = studentlist;
student *tail = studentlist;
if (studentlist != NULL) {
tail = studentlist->retnext();
}
newstud->setname(tname);
if (studentlist == NULL) {
newstud->setnext(NULL);
studentlist = newstud;
return;
}
else {
if (strcmp(head->retname(), newstud->retname()) > 0) {
newstud->setnext(studentlist);
studentlist = newstud;
return;
}
if ((strcmp(head->retname(), newstud->retname()) < 0) && (tail == NULL)) {
head->setnext(newstud);
newstud->setnext(NULL);
return;
}
while (tail != NULL && strcmp(tail->retname(), newstud->retname()) < 0) {
if (strcmp(tail->retname(), newstud->retname()) > 0) {
newstud->setnext(tail);
head->setnext(newstud);
return;
}
tail = tail->retnext();
head = head->retnext();
if (tail == NULL) {
head->setnext(newstud);
newstud->setnext(NULL);
}
}
}
}