0
53views
Implement a menu-driven program to perform the stack operations like push, pop, display, isempty, and isfull using array in C language.
1 Answer
0
2views

Menu-driven Stack Program

  • The Stack is an Abstract Data Type in which the addition of an element to the collection is called PUSH and the removal of an element called POP.

  • Stack follows a Last-In-First-Out (LIFO) data structure, the last element added to the structure must be the first one to be removed.

  • A stack may be implemented to have a bounded capacity.

  • If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an Overflow State.

  • The pop operation removes an item from the top of the stack.

  • A pop either reveals previously concealed items or results in an empty stack, but, if the stack is empty, it goes into an Underflow State, which means no items are present in the stack to be removed.


Menu-driven C Program to Implement Stack Operations using Array -

#include<stdio.h>
#include<conio.h>
#define max 20
int stack[max];
int top=0,x;
void push(int);
int pop();
void display();
int isempty();
int isfull();
void main()
{
int ch,item,d;
char a;
printf("\n\t Stack Implementation");
printf("\n\t --------------------");
printf("\n\t1.PUSH");
printf("\n\t2.POP");
printf("\n\t3.Display");
printf("\n\t4.IsEmpty");
printf("\n\t5.IsFull");
do{
printf("\n\t Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter an Element to PUSH: \n");
           scanf("%d",&item);
           push(item);
           break;
case 2:x=pop();
           printf("The element POP out from Stack is %d",x);
           break;
case 3:display();
           break;
case 4:x=isempty();
          if(x==1)
          printf("Stack is Empty");
          else
          printf("Stack is Not Empty");
          break;
case 5:x=isfull();
          if(x==1)
          printf("Stack is Full");
          else
          printf("Stack is Not Full");
          break;
default:printf("INVALID Choice\n");
}
printf("\n do u want to continue y/n: ");
scanf(" %c",&a);
}while((a=='y')||(a=='Y'));
getch();
}

void push(int x)           // PUSH function
{
if(top>=max)
printf("Stack is OVERTFLOW\n");
else
{
stack[top]=x;
top++;
}
}

int pop()           // POP function
{
if(top<=0)
printf("Stack is UNDERFLOW\n");
else
{
top--;
x=stack[top];
}
return x;
}

void display()           // Display function
{
int i;
i=top-1;
if(top<=0)
printf("Stack is Empty");
else
printf("The Elments in Stack are \n");
while(i>=0)
{
printf(" %d\n",stack[i--]);
}
}

int isempty()           // isempty function
{
if(top<=0)
return 1;
else
return 0;
}

int isfull()           // isfull function
{
if(top>=max)
return 1;
else
return 0;
}

Output of the Program -

Initially, the Stack is empty therefore, the program will show the following output for the operations like display, isempty, isfull, and POP

1

The below output shows the PUSH operation in the Stack that pushes 5 elements 10, 20, 30, 40, and 50 into the stack.

2

The below output shows the POP operation in the Stack that pops out the top element from the stack which is 50 in this case.

3

Now, here at this point isempty and isfull operation show following output.

4

Finally, Stack is full with MAX allocated size, here in the program it is 20. Therefore, PUSH and isfull operations show the below output now.

5

Please log in to add an answer.