C++ programs to implement the Stack ADT using an array

Wednesday, March 10th, 2010

/* Write C++ programs to implement the Stack ADT using an array */

#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

Avatar of Ranjith

Author Name :
Ranjith

Total : 8 Comments


8 Responses to “C++ programs to implement the Stack ADT using an array”

  1. megha duvvuri says:

    its quite simple…..and easily understandable

  2. KHALID ANSARI says:

    first of all error occured is “function exit(0) must have prototype” but however if you will not include case 4 then it wud run but after running first you enter the choice 2 n see what happend..prog is not abs. right.
    Dear uploader, upload absolutely right prog without any error..

  3. priya says:

    thanks

  4. kanaga says:

    thanks

  5. vivek says:

    it’s so easy to understand

  6. anjali says:

    its just a great piece of work.. thanx soo muchh!!

  7. ashu says:

    its too good v cn understnd it easily..

  8. merry says:

    thanks

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: