Hi,I am having a hard time trying to do insertion sort on a singly link list containing single alphabets.I have spent hours doing this but still it wont work.Any help will be apprecited.
Here is my code:
# include <iostream>
using namespace std;
class node
{
public:
char info;
node *next;
node(char e1,node *ptr=0)
{
info=e1;
next=ptr;
}
};
class linklist
{
private:
node *head;
public:
linklist()
{
head=0;
}
void addtohead(char e1)
{
head=new node(e1,head);
}
void Printlist()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->next;
}
}
void insertionsort()
{
node *i,*j;
char min;
for(i=head->next;i!=NULL ;i=i->next)
{
min=i->info;
for(j=i->next;j!=NULL && j->info > min ;j=j->next)
{
i->info=j->info;
j->info=min;
}
}
}
};
int main()
{
int count;
char c;
cout<<"How many nodes you want to create?"<<endl;
cin>>count;
linklist list;
for(int i=0;i<count;i++)
{
cout<<"Enter node "<<i+1<<": "<<endl;
cin>>c;
list.addtohead(c);
}
list.insertionsort(count);
cout<<"the sorted link list using insertion sort is:"<<endl;
list.Printlist();
system("pause");
return 0;
}