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.push\n2.pop\n3.peep\n4.display\n5.exit\n");
printf("\nEnter your choice\n");
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]);
}

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation