// A Program that exercise the operations on Stack Implementing Array
// i.e. (Push, Pop, Traverse)
#include <iostream>
#include <stdlib.h>
using namespace std;
#define STACKSIZE 10 // int const STACKSIZE = 10;
// global variable and array declaration
int Top=-1;
int Stack[STACKSIZE];
void Push(int); // functions prototyping
int Pop(void);
bool IsEmpty(void);
bool IsFull(void);
void Traverse(void);
int main( )
{ int item, choice;
while( 1 )
{
cout<< "\n\n\n\n\n";
cout<< " ******* STACK OPERATIONS ********* \n\n";
cout<< " 1- Push item \n 2- Pop Item \n";
cout<< " 3- Traverse / Display Stack Items \n 4- Exit.";
cout<< " \n\n\t Your choice ---> ";
cin>> choice;
switch(choice)
{ case 1: if(IsFull())cout<< "\n Stack Full/Overflow\n";
else
{ cout<< "\n Enter a number: "; cin>>item;
Push(item); }
break;
case 2: if(IsEmpty())cout<< "\n Stack is empty) \n";
else
{item=Pop();
cout<< "\n deleted from Stack = "<<item<<endl;}
break;
case 3: if(IsEmpty())cout<< "\n Stack is empty) \n";
else
{ cout<< "\n List of Item pushed on Stack:\n";
Traverse();
}
break;
case 4: exit(0);
default:
cout<< "\n\n\t Invalid Choice: \n";
} // end of switch block
} // end of while loop
} // end of of main() function
void Push(int item)
{ if (Top == STACKSIZE-1)
{
cout << "the stack is overflow " << endl;
}
else {
Top++;
Stack[Top]=item; }
}
int Pop( )
{ if (Top == -1 ) {
cout << " the stack is underflow " << endl ;
}
else {
return Stack[Top--]; }
}
bool IsEmpty( )
{ if(Top == -1 ) return true else return false;
}
bool IsFull( )
{ if(Top == STACKSIZE-1 ) return true else return false;
}
void Traverse( )
{ if (Top == -1 ) {
cout << " the stack is underflow " << endl ;
}
else {
for (int i = Top ; i>= 0 ;i-- ) {
cout << Stack [i] << endl ;
}
}
}