Learn How PUSH and POP Operation Works

## Algorithm for PUSH Operation

Step 1: If TOP >= SIZE – 1 then
Write “Stack is Overflow”
Step 2: TOP = TOP + 1
Step 3: STACK [TOP] = X

## Algorithm for POP Operation

Step 1: If TOP = -1 then
Write “Stack is Underflow”
Step 2: Return STACK [TOP]
Step 3: TOP = TOP - 1

## Program for Stack Operations

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
void push(int *STACK,int x);
void pop(int *a);
void display(int *a);
int TOP=-1;
int STACK[SIZE];
void main()
{
int x,ch;
clrscr();
while(1)
{
printf("(1) PUSH\n");
printf("(2) POP\n");
printf("(3) Display\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:
exit(0);
}
}
}
void push(int *STACK,int x)
{
if(TOP>=SIZE-1)
printf("Stack is Overflow");
else
{
TOP=TOP+1;
STACK[TOP]=x;
}
}
void pop(int *STACK)
{
int x;
if(TOP==-1)
printf("Stack is Underflow");
else
{
printf("Deleted Element is %d\n",STACK[TOP]);
TOP=TOP-1;
}
}
void display(int *STACK)
{
int i;
if(TOP==-1)
printf("Stack is Empty\n");
else
{
for(i=TOP;i>=0;i--)
printf("%d\n",STACK[i]);
}
}