C++ Programming Blog

 
 
 

Image

 # 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_list( );
      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_list( )  -------------------------//
 /**************************************************************************/

 void Linked_list_Stack::print_list( )
    {
       print=bottom;

       if(print!=NULL)
      cout<<\"\\n\\n\\n\\n\\n\\t Values pushed onto Stack are : \\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( );
    }

 /**************************************************************************/
 //---------------------------  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 values\"<<endl;

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

         Input:

         gotoxy(10,20);
         cout<<\"                      \";

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

         Key=getche( );

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

         else if(Key==\'p\' || Key==\'P\')
        push( );

         else if(Key==\'o\' || Key==\'O\')
        pop( );

         else if(Key==\'s\' || Key==\'S\')
        print_list( );

         else
        goto Input;
      }
       while(1);
    }


 int main( )
    {
       Linked_list_Stack  obj;

       obj.show_working( );

       return 0;
    }

 
 
Didn't find what you were looking for? Find more on Program to illustrate the implementation of linked list as a Stack