Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to perform sorting of linklist

Posted By: Easy Tutor     Category: C++ Programming     Views: 3123

Write a Program to perform sorting of linklist.

Code for Program to perform sorting of linklist in C++ Programming

//LinkList Sorting

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

struct node{
    int data;
    struct node *next;
};

class link{
    node *head;
    public:
     void create();
     void display();
     void sort();
};


void link :: create(){
    node *newl=NULL,*end=newl;
    int data;
    head=NULL;
    cout<<"\n**Create List**\n";
    cout<<"\nEnter -999 to terminate\n";
    while(1){
       cout<<"Enter Data Value : ";
       cin>>data;
       if(data==-999)
     break;
       else{
       newl = new node;
       newl->data=data;
       if(head==NULL)
          head=newl;
       else
          end->next=newl;
       end=newl;
       end->next=NULL;
       }
    }
    cout<<"\nList is Created\n";
    getch();
}

void link :: display(){
    node *start;
    start=head;
    cout<<"\n**Display**\n";
    while(start!=NULL){
       cout<<start->data;
       if(start->next!=NULL)
           cout<<"==>";
       start=start->next;
    }
    cout<<"\nDisplay's End\n";
    getch();
}

void link :: sort(){
    cout<<"\n**Linear Sort**\n";
    node *i, *j;
    int temp;
    for(i=head;i!=NULL;i=i->next){
       for(j=i->next;j!=NULL;j=j->next){
           if(i->data > j->data){
          temp    =  i->data;
          i->data = j->data;
          j->data = temp;
           }
       }
    }
    cout<<"\nData are Sorted\n";
    getch();
}

void main(){
     int choice;
     link obj;
     while(1){
    clrscr();
    cout<<"****LinkList Sorting Demo****"<<endl;
    cout<<"1) Create List\n";
    cout<<"2) Display List\n";
    cout<<"3) Sort List\n";
    cout<<"4) Exit\n";
    cout<<"Enter your choice : ";
    cin>>choice;
    switch(choice){
         case 1 :  obj.create();
               break;
         case 2 :  obj.display();
            break;
         case 3 : obj.sort();
            break;
         case 4 : gotoout;
         default : cout<<"\nInvalid Choice\n";
    }
     }
out:
}
  
Share: 


Didn't find what you were looking for? Find more on Program to perform sorting of linklist Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to perform sorting of linklist is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!