#include<stdio.h>
//Stack size
#define SIZE 5
//Declare stack functions
void push(int *STACK,int x);
void pop(int *a);
void display(int *a);
//Decalre TOP and STACK variables
int TOP=-1;
int STACK[SIZE];
void main()
{
int x,ch;
//implement continues while loop using 1
while(1)
{
printf("Select Your Choice:\n");
printf("(1) PUSH\n");
printf("(2) POP\n");
printf("(3) Display\n");
printf("(4) Enter 4 to exit \n");
printf("Enter Your Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter Element:");
scanf("%d",&x);
push(STACK,x);
break;
case 2:
pop(STACK);
break;
case 3:
display(STACK);
break;
case 4:
//break loop using exit
exit(0);
}
}
}
void push(int *STACK,int x)
{
//check if TOP value (TOP=current stack value) is greater than or equal SIZE
// if yes Stack Overlfowed cannot enter new data in STACK
if(TOP>=SIZE-1)
{
printf(" Stack Overflowed ");
}
//if there is more space in STACK eneter data
else
{
TOP=TOP+1;
STACK[TOP]=x;
}
}
void pop(int *STACK)
{
int x;
//check is TOP (TOP=current stack value) of stack is equal to -1
//then is it already empty
if(TOP==-1){
printf("Stack is Underflow");
}
//remove top element from Stack
else
{
printf("Deleted Element is %d\n",STACK[TOP]);
//decrement TOP value
TOP=TOP-1;
}
}
void display(int *STACK)
{
int i;
//check is TOP (TOP=current stack value) of stack is equal to -1
//then is it already empty
if(TOP==-1){
printf("Stack is Empty\n");
}
else
{
printf ("\n Stack element are \n");
//loop through each value of stack and print it
for(i=TOP;i>=0;i--)
{
printf("%d\t",STACK[i]);
}
}
}