Write a recursive program to display elements of stack using the PUSH() and POP() operations and without violating the LIFO concept.
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int top=-1,stack[MAX];
void display();
void pop();
void push();
void main()
{
int x;
while(1)
{
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nSelect a choice:");
scanf("%d",&x);
switch(x)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nInvalid Choice!!");}}
}
void push()
{
int ff;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter the element to push:");
scanf("%d",&ff);
top=top+1;
stack[top]=ff;
}
}
void pop()
{
if(top==-1)
{
printf("\nCannot pop Stack is empty!!");
}
else
{
printf("\nElement deleted is %d",stack[top]);
top=top-1;
}
}
void display()
{
int m;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nContent of the stack:\n");
for(m=top;m>=0;--m)
printf("%d\n",stack[m]);
}
}
Comments
Leave a comment