C++ program to sort a linked list using bubble sort.
C++ program to sort a linked list
#include<iostream>
#include<conio.h>
using namespace std;
struct links
{
int data;
links *next;
};
class linkedlist
{
links *start;
public:
void create();
int countnode();
void sort();
void display();
};
int linkedlist::countnode()
{
int count=0;
links *temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
count++;
}
return count+1;
}
void linkedlist::sort()
{
links *temp,*temp1,*temp2;
for(int i=countnode();i>0;i--)
{
temp1=start->next;
temp2=start;
while(temp1!=NULL)
{
if(temp2->data>temp1->data)
{
if(temp2==start)
{
temp=start=temp1;
temp2->next=temp1->next;
temp1->next=temp2;
temp1=temp2->next;
}
else
{
temp->next=temp1;
temp2->next=temp1->next;
temp1->next=temp2;
temp1=temp2->next;
temp=temp->next;
}
}
else
{
temp=temp2;
temp2=temp1;
temp1=temp1->next;
}
}
}
}
void linkedlist::create()
{
int take;
links *temp;
char ch;
cout<<"Enter the element ";
cin>>take;
start=new links;
start->data=take;
start->next=NULL;
temp=start;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the element ";
cin>>take;
temp->next=new links;
temp=temp->next;
temp->data=take;
temp->next=NULL;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
}
}
void linkedlist::display()
{
links *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<"-> ";
temp=temp->next;
}
cout<<"NULL\n";
}
int main()
{
linkedlist sortlist;
cout<<"Create a list "<<endl;
sortlist.create();
cout<<"\nBefore sorting linked list\n ";
sortlist.display();
sortlist.sort();
cout<<"\nAfter sorting linked list\n";
sortlist.display();
return 0;
}
#include<conio.h>
using namespace std;
struct links
{
int data;
links *next;
};
class linkedlist
{
links *start;
public:
void create();
int countnode();
void sort();
void display();
};
int linkedlist::countnode()
{
int count=0;
links *temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
count++;
}
return count+1;
}
void linkedlist::sort()
{
links *temp,*temp1,*temp2;
for(int i=countnode();i>0;i--)
{
temp1=start->next;
temp2=start;
while(temp1!=NULL)
{
if(temp2->data>temp1->data)
{
if(temp2==start)
{
temp=start=temp1;
temp2->next=temp1->next;
temp1->next=temp2;
temp1=temp2->next;
}
else
{
temp->next=temp1;
temp2->next=temp1->next;
temp1->next=temp2;
temp1=temp2->next;
temp=temp->next;
}
}
else
{
temp=temp2;
temp2=temp1;
temp1=temp1->next;
}
}
}
}
void linkedlist::create()
{
int take;
links *temp;
char ch;
cout<<"Enter the element ";
cin>>take;
start=new links;
start->data=take;
start->next=NULL;
temp=start;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the element ";
cin>>take;
temp->next=new links;
temp=temp->next;
temp->data=take;
temp->next=NULL;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
}
}
void linkedlist::display()
{
links *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<"-> ";
temp=temp->next;
}
cout<<"NULL\n";
}
int main()
{
linkedlist sortlist;
cout<<"Create a list "<<endl;
sortlist.create();
cout<<"\nBefore sorting linked list\n ";
sortlist.display();
sortlist.sort();
cout<<"\nAfter sorting linked list\n";
sortlist.display();
return 0;
}
No comments:
Post a Comment