Hello!
I am writing a code of linked list in which user can insert or delete names but the list must remain sorted alphabetically. I have written the given piece of code by now but it is not correct. Please help me out.
I took help from this link:
http://stackoverflow.com/questions/2941368/sorting-names-in-a-linked-list
//function to add a node
void add(node *&pi,string newname)
{
node *temp;
temp = new node;
if(p== NULL)
{
temp->info= newname;
temp->link=p;
p=temp;
}
else
{
node *add;
add = new node;
bool done=true;
while (p!=NULL)
{
if((p->info)>newname)
{
add->info= newname;
add->link= p->link;
done=true;
break;
}
p=p->link;
}
if(done==false)
{
node *anew;
anew=new node;
anew->info=newname;
anew->link=NULL;
if(head==NULL)
{
head=anew;
tail=anew;
count++;
}
else
{
tail->link=anew;
tail=anew;
count++;
}
}
}
}