/* Below Stack implementation program is written in C++ language */
# include<iostream>
using namespace std;
const int n=10;
int top;
int Stack[n]; //Maximum size of Stack
// declaring all the function
void push(int x);
int pop();
bool isFull();
bool isEmpty();
void printStack();
// function to insert data into stack
void push(int x)
{
if (isFull()) cout << "Stack Overflow \n";
else
{
top = top + 1;
Stack[top] = x;
}
}
// function to remove data from the top of the stack
int pop()
{
int d;
if(isEmpty()) cout << "Stack Underflow \n";
else
{
d = Stack[top];
top = top -1;
return d;
}
}
// function to check if stack is full
bool isFull()
{
if(top >= n ) return true;
else return false;
}
// function to check if stack is empty
bool isEmpty()
{
if(top < 0 ) return true;
else return false;
}
// function to print stack elements
void printStack()
{
if (isEmpty()) cout << "Stack Underflow \n";
else
for (int i=0; i<=top; i++)
cout<<"stack["<<i<<"]="<<Stack[i]<<endl;
}
// main function
int main()
{
int c,m;
top=-1;
do
{
cout<<"Enter 1 to insert new element "<<endl;
cout<<"Enter 2 to delete top element "<<endl;
cout<<"Enter 3 to print stack elements "<<endl;
cout<<"Enter 4 to Exit "<<endl;
cout<<"Enter your choice :";
cin>>c;
switch (c)
{
case 1 : cout<<"Enter new elements : ";
cin>>m;
push(m);
break;
case 2 : m = pop();
if (! isEmpty())cout<<"deleting element
"<<m<<endl;break;
case 3 : printStack();break;
case 4 : exit;break;
default : cout<<" Error choice !!"<<endl;
}
} while (c != 4);
}