Write a C program of Stack menu driven using if-else statements and switch case.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define max 5
int top=-1,s[max];
void push(int n)
{
if(top==max-1)
{
puts("Stack is overflown");
return;
}
else
{ top=top+1;
s[top]=n;
} }
void pop()
{
int del;
if(top==-1)
{
puts("Stack is underflown");
return; }
else
{
del=s[top];
printf("\n poped element is %d",del);
top=top-1;
} }
void display()
{
int i;
if(top==-1)
puts("stack is empty");
else
{ for(i=top;i>=0;i--)
printf("\t %d",s[i]);
} }
int main()
{ int opt,n;
do
{
printf("\n 1. push");
printf("\n 2. pop");
printf("\n 3. display");
printf("\n 4. exit");
printf("\n enter your choice :: ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\n enter any element to push :: ");
scanf("%d",&n);
push(n);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
} }
while(1);
return 0;
}
0 Comments
Have any query? Want any type of module?
Please comment it down and let me know.