c program to implement stack to perform push(insert) peep(display first element) and pop(delete).

#include<stdio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]); // peep display the topmost element from stack
void display(int st[]);
int main()
{
int val,option;
do
{
printf(“n*****stack menu*****n”);
printf(“n1.pushn2.popn3.peepn4.displayn5.exitn”);
printf(“nEnter your choicen”);
scanf(“%d”,&option);
switch(option)
{
case 1:
printf(“nEnter the number to be pushed on to the stack:”);
scanf(“%d”,&val);
push(st,val); //fn call
break;
case 2:val=pop(st);
printf(“nThe value deleted from the stack is:%d”,val);
break;
case 3:
val=peep(st);
printf(“n The value stored at the top of the stack is:%d”,val);
break;
case 4:display(st);
break;
}
}while (option!=5);
return 0;
}
void push(int st[],int val)
{
if(top==MAX-1)
{
printf(“n Stack overflow”);
}
else
{
top++;
st[top]=val;
}
}
int pop(int st[])
{
int val;
if(top==-1)
{
printf(“n stack underflow”);
return -1;
}
else
{
val=st[top];
top–;
return val;
}
}
void display(int st[])
{
int i;
if(top==-1)
printf(“n stack is empty”);
else
{
for(i=top;i>=0;i–)
printf(“n%d”,st[i]);
}
}
int peep(int st[])
{
if(top == NULL)
{
printf(“nstack is empty”);
return -1;
}
else
return (st[top]);
}

Leave a Comment

Your email address will not be published. Required fields are marked *

To Resolve this issue at just 5$ from ServerExpert

https://serverexpert.io/

For Hosting please visit Hostingwebsite.io

You have Successfully Subscribed!

Open chat