answersLogoWhite

0

#include

#include

#include

#define MAXSIZE 50

int a[MAXSIZE];

int top=-1;

void main()

{

int choice;

void push();

int pop();

void display();

clrscr();

do

{

printf("\nYou can do following operations on stack\n");

printf("\n1. Push");

printf("\n2. Pop");

printf("\n3. Display");

printf("\n4. Exit");

printf("\n Enter your choice");

scanf("%d",&choice);

switch(choice)

{

case 1: push();

break;

case 2: pop();

break;

case 3: display();

break;

case 4: break;

default: printf("\n Wrong input");

}

}while(choice!=4);

getch();

}

void push()

{

int value;

if(top==MAXSIZE-1)

{

printf("\n Stack is overflow");

}

else

{

printf("\n Enter the value which you want to insert");

scanf("%d",&value);

top++;

a[top]=value;

}

}

int pop()

{

int value;

if(top==-1)

{

printf("\nStack is underflow");

return(0);

}

else

{

value=a[top];

top--;

}

return value;

}

void display()

{

int i;

if(top==-1)

{

printf("\n Stack is underflow");

}

else

{

for(i=top;i>=0;i--)

{

printf("\n%d",a[i]);

}

}

}
#include < stdio.h >

#include < conio.h >

#define MAX 10

int stack[MAX],top;

void main()

{

char ch;

int choice,item;

void display();

int pop();

void push(int);

clrscr();

top=-1;

do

{

printf("1.Insertion");

printf("\n2.Deletion");

printf("\n3.Display");

printf("\nEnter your choice(1-3):");

scanf("%d",&choice);

switch(choice)

{

case 1: printf("\nEnter the item to be inserted:");

scanf("%d",&item);

push(item);

display();

break;

case 2: item=pop();

printf("\nThe item deleted is %d",item);

display();

break;

case 3: display();

break;

default:printf("Wrong choice");

break;

}

printf("\nDo you want to continue(y/n):");

fflush(stdin);

scanf("%c",&ch);

}while(ch=='y' ch=='Y');

getch();

}

void push(int item)

{

if(top==MAX-1)

{

printf("Stack Overflow");

return;

}

top=top+1;

stack[top]=item;

}

int pop()

{

int t;

if(top==-1)

{

printf("Stack Underflow");

return -1;

}

t=stack[top];

top=top-1;

return t;

}

void display()

{

int i;

printf("\nThe Stack elements are:");

for(i=0;i < =top;i++)

printf("%5d",stack[i]);

}

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
BeauBeau
You're doing better than you think!
Chat with Beau

Add your answer:

Earn +20 pts
Q: What is a Menu driven program for a stack operation?
Write your answer...
Submit
Still have questions?
magnify glass
imp