Answer to Question #236748 in C for SOM

Question #236748

Write a recursive program to display elements of stack using the PUSH() and POP() operations and without violating the LIFO concept.



1
Expert's answer
2021-09-14T00:41:44-0400
#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]);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS