Stacks Using Arrays
#include<stdio.h>
#include<stdlib.h>
#define max 5
int top;
int stack[max];
void push(int value)
{
if(top==max-1)
{
printf("overflow push not possible");
return;
}
top=top+1;
stack[top]=value;
}
int pop()
{
int x;
if(top==-1)
{
printf("underflow exists");
return(-1);
}
x=stack[top];
top=top-1;
return(x);
}
int peep()
{
int x;
if(top==-1)
{
printf("stack empty");
return(-1);
}
x=stack[top];
return(x);
}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty");
return;
}
for(i=top;i>=0;i--)
printf("\t%d \t",stack[i]);
}
void main()
{
int x, choice;
top=-1;
while(1)
{
printf("menu 1:push 2: pop 3:peep 4: exit");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter element to be inserted");
scanf("%d",&x);
push(x);
display();
break;
case 2: x=pop();
printf("deleted element is %d",x);
printf("\t remaining stack is");
display();
break;
case 3: x=peep();
printf("top element is %d",x);
break;
default: exit(0);
}
}
}