linked list bubble sort

sahasrara 0 Tallied Votes 214 Views Share

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
{
    friend class linklist;
    int number;
    node *next;
};

class linklist
{
    private:
        node *first;
        node *last;
    public:
    	linklist(){ first = NULL; }
        ~linklist(){};
        void addnode();
        void BubbleSort();
        void displaynode();

    };

void linklist :: addnode()
{
  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);
}

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

void linklist ::BubbleSort()
{
  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()
{
    linklist sl;
    sl.addnode();
    sl.BubbleSort();
    sl.displaynode();
    getch();
}
Sahilroy -7 Newbie Poster
//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
{
    friend class linklist;
    int number;
    node *next;
};

class linklist
{
    private:
        node *first;
        node *last;
    public:
    	linklist(){ first = NULL; }
        ~linklist(){};
        void addnode();
        void BubbleSort();
        void displaynode();

    };

void linklist :: addnode()
{
  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);
}

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

void linklist ::BubbleSort()
{
  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()
{
    linklist sl;
    sl.addnode();
    sl.BubbleSort();
    sl.displaynode();
    getch();
}
Sahilroy -7 Newbie Poster

hi i changed the program to C++ enjoys

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

** 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;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.