Logo 
Search:

C++ Programming Articles

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

Program to print a Single Ended Linked List in Original & Reverse order and sort it in Ascending & Decending Order

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

Writ a program to print a Single Ended Linked List in Original & Reverse order and sort it in Ascending & Decending Order.

Code for Program to print a Single Ended Linked List in Original & Reverse order and sort it in Ascending & Decending Order in C++ Programming

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

 class Linked_list_Stack
    {
       private:
      struct node
         {
        int data;

        node *next;
         };

      node *top;
      node *entry;
      node *print;
      node *bottom;
      node *last_entry;
      node *second_last_entry;

       public:
      Linked_list_Stack( );

      void pop( );
      void push( );
      void print_original_list( );
      void print_reverse_list( );
      void sort_list_in_ascending_order( );
      void sort_list_in_decending_order( );
      void show_working( );
    };


 /**************************************************************************///----------------------  Linked_list_Stack( )  --------------------------///**************************************************************************/

 Linked_list_Stack::Linked_list_Stack ( )
    {
       top=NULL;
       bottom=NULL;
    }

 /**************************************************************************///-------------------------------  push( )  ------------------------------///**************************************************************************/void Linked_list_Stack::push( )
    {
       int num;

       cout<<"\n\n\n\n\n\t Enter value to push onto Stack : ";
       cin>>num;

       entry=new node;

       if(bottom==NULL)
      {
         entry->data=num;
         entry->next=NULL;
         bottom=entry;
         top=entry;
      }

       else
      {
         entry->data=num;
         entry->next=NULL;
         top->next=entry;
         top=entry;
      }

       cout<<"\n\n\t *** "<<num<<" is pushed onto the Stack."<<endl;
       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /**************************************************************************///----------------------------  pop( )  ----------------------------------///**************************************************************************/void Linked_list_Stack::pop( )
    {
       if(bottom==NULL)
      cout<<"\n\n\n\t ***  Error : Stack is empty. \n"<<endl;

       else
      {
         for(last_entry=bottom;last_entry->next!=NULL;
                         last_entry=last_entry->next)
        second_last_entry=last_entry;

         if(top==bottom)
        bottom=NULL;

         int poped_element=top->data;

         delete top;

         top=second_last_entry;
         top->next=NULL;

         cout<<"\n\n\n\t *** "<<poped_element<<" is poped from the Stack."<<endl;
      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /**************************************************************************///----------------------  print_original_list( )  ------------------------///**************************************************************************/void Linked_list_Stack::print_original_list( )
    {
       print=bottom;

       if(print!=NULL)
      cout<<"\n\n\n\n\n\t The List in original order is : \n"<<endl;

       else
      cout<<"\n\n\n\n\n\t *** Nothing to show. "<<endl;

       while(print!=NULL)
      {
         cout<<"\t "<<print->data<<endl;

         print=print->next;
      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /**************************************************************************///-----------------------  print_reverse_list( )  ------------------------///**************************************************************************/void Linked_list_Stack::print_reverse_list( )
    {
      if(bottom==NULL)
      cout<<"\n\n\n\n\n\t *** Nothing to show. "<<endl;

      else
      {
          cout<<"\n\n\n\n\n\t The List in reverse order is : \n"<<endl;

          node *temp;

          last_entry=top->next;

          for(print=bottom;print!=NULL;print=print->next)
          {
             for(temp=bottom;temp!=last_entry;temp=temp->next)
                second_last_entry=temp;

             last_entry=second_last_entry;

             cout<<"\t "<<last_entry->data<<endl;
          }
      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /**************************************************************************///-----------------  sort_list_in_ascending_order( )  --------------------///**************************************************************************/void Linked_list_Stack::sort_list_in_ascending_order( )
    {

      if(bottom==NULL)
      cout<<"\n\n\n\n\n\t *** Nothing to sort. "<<endl;

      else
      {
         node *temp;

          for(print=bottom;print!=NULL;print=print->next)
          {
             for(temp=bottom;temp!=NULL;temp=temp->next)
                {
                   int data_1=print->data;
                   int data_2=temp->data;

                   if(data_1<data_2)
                      {
                         print->data=data_2;
                         temp->data=data_1;
                      }
                }
          }

          print=bottom;

          cout<<"\n\n\n\n\n\t The linked list in Ascending order is : \n"<<endl;

          while(print!=NULL)
          {
         cout<<"\t "<<print->data<<endl;

         print=print->next;
         }

      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /**************************************************************************///-----------------  sort_list_in_decending_order( )  --------------------///**************************************************************************/void Linked_list_Stack::sort_list_in_decending_order( )
    {

      if(bottom==NULL)
      cout<<"\n\n\n\n\n\t *** Nothing to sort. "<<endl;

      else
      {
         node *temp;

          for(print=bottom;print!=NULL;print=print->next)
          {
             for(temp=bottom;temp!=NULL;temp=temp->next)
                {
                   int data_1=print->data;
                   int data_2=temp->data;

                   if(data_1>data_2)
                      {
                         print->data=data_2;
                         temp->data=data_1;
                      }
                }
          }

          print=bottom;

          cout<<"\n\n\n\n\n\t The linked list in Decending Order is : \n"<<endl;

          while(print!=NULL)
          {
         cout<<"\t "<<print->data<<endl;

         print=print->next;
         }

      }

       cout<<"\n\n\n\t\t Pres any key to return to Menu. ";

       getch( );
    }

 /**************************************************************************///---------------------------  show_working( )  --------------------------///**************************************************************************/void Linked_list_Stack::show_working( )
    {
       char Key=NULL;

       do
      {
         clrscr( );

         gotoxy(5,5);
         cout<<"**********   Implementation of Linked List as a Stack   **********"<<endl;

         gotoxy(10,8);
         cout<<"Select one of the listed operation :"<<endl;

         gotoxy(15,10);
         cout<<"- Press \'P\' to Push a value"<<endl;

         gotoxy(15,12);
         cout<<"- Press \'O\' to Pop a value"<<endl;

         gotoxy(15,14);
         cout<<"- Press \'S\' to Print the list in Original order"<<endl;

         gotoxy(15,16);
         cout<<"- Press \'R\' to Print the list in Reverse order"<<endl;

         gotoxy(15,18);
         cout<<"- Press \'A\' to Sort the list in Ascending order"<<endl;

         gotoxy(15,20);
         cout<<"- Press \'D\' to Sort the list in Decending order"<<endl;

         gotoxy(15,22);
         cout<<"- Press \'E\' to Exit"<<endl;

         Input:

         gotoxy(10,26);
         cout<<"                      ";

         gotoxy(10,26);
         cout<<"Enter your Choice : ";

         Key=getche( );

         if(int(Key)==27 || Key=='e' || Key=='E')
        break;

         elseif(Key=='p' || Key=='P')
        push( );

         elseif(Key=='o' || Key=='O')
        pop( );

         elseif(Key=='s' || Key=='S')
        print_original_list( );

         elseif(Key=='r' || Key=='R')
        print_reverse_list( );

         elseif(Key=='a' || Key=='A')
        sort_list_in_ascending_order( );

         elseif(Key=='d' || Key=='D')
        sort_list_in_decending_order( );

         elsegoto Input;
      }
       while(1);
    }

 

 int main( )
    {
       Linked_list_Stack  obj;

       obj.show_working( );

       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to print a Single Ended Linked List in Original & Reverse order and sort it in Ascending & Decending Order 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!