C++ programs – Implement the Stack ADT using an array

by Nideesh C on April 14, 2011 · 0 comments

in C++




#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
             int stk[5];
             int top;
      public:
             stack()
              {
                top=-1;
               }
             void push(int x)
              {
                 if(top >  4)
                       {
                           cout <<"stack over flow";
                           return;
                       }
                 stk[++top]=x;
                 cout <<"inserted" <<x;
               }
             void pop()
               {
                  if(top <0)
                   {
                         cout <<"stack under flow";
                         return;
                    }
                    cout <<"deleted" <<stk[top--];
                }
             void display()
               {
                   if(top<0)
                    {
                            cout <<" stack empty";
                            return;
                    }
                    for(int i=top;i>=0;i--)
                    cout <<stk[i] <<" ";
                }
};
 
main()
{
     int ch;
     stack st;
     while(1)
        {
             cout <<"\n1.push  2.pop  3.display  4.exit\nEnter ur choice";
             cin >> ch;
             switch(ch)
              {
               case 1:  cout <<"enter the element";
                        cin >> ch;
                        st.push(ch);
                        break;
               case 2:  st.pop();  break;
               case 3:  st.display();break;
               case 4:  exit(0);
               }
         }
return (0);
}

OUTPUTS
 1.push  2.pop  3.display  4.exit
 Enter ur choice2
 stack under flow
 1.push  2.pop  3.display  4.exit
 Enter ur choice1
 enter the element2

inserted2
1.push  2.pop  3.display  4.exit
 Enter ur choice1
 enter the element3
 inserted3

1.push  2.pop  3.display  4.exit
 Enter ur choice2
 deleted3

1.push  2.pop  3.display  4.exit
 Enter ur choice1
 enter the element5
 inserted5

1.push  2.pop  3.display  4.exit
 Enter ur choice3
 5 2

1.push  2.pop  3.display  4.exit
 Enter ur choice4



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ programs – Implement the Stack ADT using a singly linked list
  2. C program to implement stack. Stack is a LIFO data strcuture LIFO – Last in First Out Perform PUSH(insert operation), POP(Delete operation) and Display stack
  3. C++ programs – Implement the Queue ADT using an array
  4. C++ program – Implement circular queue ADT using an array
  5. C++ program to implement Stack using Formula Based Representation

Leave a Comment

Previous post:

Next post: