Practical 1 ( A ). Implementation of stack using menu driven program.
#include<stdio.h>
#define max 5
int stack[max];
int top=-1;
void push(int x)
{
if(top==max-1){
printf("Overflow");
}
else{
top++;
stack[top]=x;
printf("Element pushed in stack\n");
}
}
void pop()
{
if(top==-1){
printf("Underflow");
}
else{
printf("Element poped: %d",stack[top]);
top--;
}
}
void peek()
{
if(top==-1){
printf("Underflow");
}
else{
printf("Top of the stack: %d",stack[top]);
}
}
void display()
{
if(top==-1){
printf("Underflow");
}
else{
for(int i=top;i>=0;i--){
printf("elements: %d\t",stack[i]);
}
}
}
int main()
{
int choice,value;
do{
printf("Menu\n");
printf("1.Push\n 2.Pop\n 3.Peek\n 4.Display\n 5.Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value:");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid Input\n");
}
}while(choice!=5);
}
Practical 1 ( A ). Implementation of stack using menu driven program.
Reviewed by RISHI and ARYAN
on
20:58
Rating:

No comments:
May I help you ?