Dear all
this program create a linked list of numbers while you dont enter 0.
then bubble sort function is called and finally display function shows sorted list.
I wrote it by Borland 5.02 .
Have a good time!

``````//linked list bubble sort
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/1/13

#include <iostream>
#include <conio>

class node
{
int number;
node *next;
};

{
private:
node *first;
node *last;
public:
void BubbleSort();
void displaynode();

};

{
node *newno;
do
{
newno = new node;
newno->next=NULL;
cout<<"Number : ";
cin>>newno->number;
if(first==NULL)
first=last=newno;
else
{
last->next=newno;
last=newno;
}
}while(newno->number != 0);
}

{
cout<<"---------------------\nSorted numbers :\n";
node *curno=first;
while(curno)
{
cout<<curno->number<<' ';
curno=curno->next;
}
}

{
int i,j,m,n=0,hold;
node *q, *p, *t;
for(node*q = first ; q ; q=q->next)   //balad shodid;
++n;

for(i=1 , t = first  ; i<=n-1 ;  t = t->next , ++i)
for( j=0 , p = first  ; j<n-i ;  p = p->next , ++j)
if(p->number > (p->next)->number)
{
hold = p->number;
p->number = (p->next)->number;
(p->next)->number = hold;
}
}

void main()
{
sl.BubbleSort();
sl.displaynode();
getch();
}``````
3
Contributors
3
Replies
8
Views
9 Years
Discussion Span
Last Post by mike_2000_17
``````//linked list bubble sort
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/1/13

#include <iostream>
#include <conio.h>

using namespace std;

class node
{
int number;
node *next;
};

{
private:
node *first;
node *last;
public:
void BubbleSort();
void displaynode();

};

{
node *newno;
do
{
newno = new node;
newno->next=NULL;
cout<<"Number : ";
cin>>newno->number;
if(first==NULL)
first=last=newno;
else
{
last->next=newno;
last=newno;
}
}while(newno->number != 0);
}

{
cout<<"---------------------\nSorted numbers :\n";
node *curno=first;
while(curno)
{
cout<<curno->number<<' ';
curno=curno->next;
}
}

{
int i,j,m,n=0,hold;
node *q, *p, *t;
for(node*q = first ; q ; q=q->next)   //balad shodid;
++n;

for(i=1 , t = first  ; i<=n-1 ;  t = t->next , ++i)
for( j=0 , p = first  ; j<n-i ;  p = p->next , ++j)
if(p->number > (p->next)->number)
{
hold = p->number;
p->number = (p->next)->number;
(p->next)->number = hold;
}
}

int main()
{
sl.BubbleSort();
sl.displaynode();
getch();
}``````

hi i changed the program to C++ enjoys

** Don't bump old threads like that, especially if you add nothing to it **

BTW, the C++ version of this code is this:

``````#include <iostream>
#include <list>
#include <algorithm>

int main() {
using namespace std;
int number;
list<int> lst;
while((cin >> number) && (number != 0))
lst.push_back(number);
lst.sort();
copy(lst.begin(),lst.end(),ostream_iterator<int>(cout," "));
return 0;
};``````

Edited by mike_2000_17: n/a

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.