sorting names in a linked list
- by sil3nt
Hi there, I'm trying to sort names into alphabetical order inside a linked list but am getting a run time error. what have I done wrong here?
#include <iostream>
#include <string>
using namespace std;
struct node{
string name;
node *next;
};
node *A;
void addnode(node *&listpointer,string newname){
node *temp;
temp = new node;
if (listpointer == NULL){
temp->name = newname;
temp->next = listpointer;
listpointer = temp;
}else{
node *add;
add = new node;
while (true){
if(listpointer->name > newname){
add->name = newname;
add->next = listpointer->next;
break;
}
listpointer = listpointer->next;
}
}
}
int main(){
A = NULL;
string name1 = "bob";
string name2 = "tod";
string name3 = "thomas";
string name4 = "kate";
string name5 = "alex";
string name6 = "jimmy";
addnode(A,name1);
addnode(A,name2);
addnode(A,name3);
addnode(A,name4);
addnode(A,name5);
addnode(A,name6);
while(true){
if(A == NULL){break;}
cout<< "name is: " << A->name << endl;
A = A->next;
}
return 0;
}