sorting names in a linked list
Posted
by sil3nt
on Stack Overflow
See other posts from Stack Overflow
or by sil3nt
Published on 2010-05-31T04:32:06Z
Indexed on
2010/05/31
4:32 UTC
Read the original article
Hit count: 310
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;
}
© Stack Overflow or respective owner