You are on page 1of 2

Design, Develop and Implement a menu driven Program in C for the following

operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. Exit
Support the program with appropriate functions for each of the above operations

Algorithm:
Step 1: Start.
Step 2: Initialize stack size MAX and top of stack -1.
Step 3: Push integer element on to stack and display the contents of the
stack. If stack is full give a message as ‘Stack Overflow’.
Step 4: Pop elements from stack along with display the stack contents.
If stack is empty give a message as ‘Stack Underflow’.
Step 5: Stop.

Program 3:
#include<stdio.h>
#include<stdlib.h>
#define max 4
int st[max],top=-1,i,ch,elm,flag;
void push()
{
if(top==(max-1))
printf("Stack Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&elm);
top++;
st[top]=elm;
}}
void pop()
{
if(top==-1)
printf("Stack Underflow\n");
else
{
elm=st[top];
top--;
printf("Popped element=%d\n",elm);
}}
void display()
{
if(top==-1)
printf("Stack is empty\n");
else
{
printf("Stack elements:\n");
for(i=top;i>=0;i--)
printf("%d\n",st[i]);
}}
void main()
{
while(1)
{
printf("\n\tMENU\n1.Push\t2.Pop\t3.Display\t4.EXIT\nEnter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Invalid choice\n");
}}}

You might also like