Logo 
Search:

C++ Programming Articles

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

Program to illustrate the implementation of linked list as a Stack

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

Write a program to illustrate the implementation of linked list as a Stack.

Code for Program to illustrate the implementation of linked list as a Stack 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_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;

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

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

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

         elsegoto Input;
      }
       while(1);
    }


 int main( )
    {
       Linked_list_Stack  obj;

       obj.show_working( );

       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to illustrate the implementation of linked list as a Stack 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

Related Articles and Code:


 
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!