#include<stdio.h>//header file
#include<conio.h>//header file
int top=-1;
void push(int *);
void pop(int *);
void display(int *);
void peep(int *);
void main() //starting of the main() function
{
int a[5]; //declaration of the variables
int i;
clrscr();
printf("program that implements the stack using pop and push function");
printf("\n\t-------------------------OUTPUT------------------------");
while(1)
{
printf("\n\nenter 1 for push");//taking input from the user
printf("\nenter 2 for pop");
printf("\nenter 3 for display");
printf("\nenter 4 for peep");
printf("\npress any other integer key for exit");
printf("\n\nenter the value");
scanf("%d",&i);
if(i==1)
push(a);
else if(i==2)
pop(a);
else if(i==3)
display(a);
else if(i==4)
peep(a);
else
{
printf("Exit");
getch();
break;
}
}
}
void push(int a[])
{
int d;
if(top==4)
{
printf("\n\nstack overflow");//printing output
}
else
{
printf("\nenter the value to be pushed");
scanf("%d",&d);
top++;
a[top]=d;
}
}
void pop(int a[])
{
int d;
if(top==-1)
{
printf("\n\nstack underflow");//printing output
}
else
{
d=a[top];
printf("\nthe value popped is=%d",d); //printing output
top--;
}
}
void display(int a[])
{
int j;
if(top==-1)
printf("\n\nstack is empty");
else
{
printf("\n\nthe values are=");//printing output
for(j=top;j>=0;j--)
printf(" %d",a[j]);
}
}
void peep(int a[])
{
int i,flag=0,ele;
printf("enter the element to be searched");
scanf("%d",&ele);
for(i=0;i<=top;i++)
{
if(a[i]==ele)
{
flag=1;
break;
}
}
if(flag==1)
printf("the element exists");
else
printf("the element does not exists");
}