Write program to implement stack with POP and PUSH operations?

1 answer

Answer

1253636

2026-05-01 11:36

+ Follow

SOURCE CODE:

#include

#include

void push(int st[],int data,int &top);

void disp(int st[],int &top);

int pop(int st[],int &top);

int flg=0;

int top=-1,tos=-1;

int st[50];

void push(int st[],int data,int &top)

{

if(top==50-1)

flg=0;

else

{

flg=1;

top++;

st[top]=data;

}

}

int pop(int st[],int &top)

{

int pe;

if(top==-1)

{

pe=0;

flg=0;

}

else

{

flg=1;

pe=st[top];

top--;

}

return(pe);

}

void disp(int st[],int &top)

{

int i;

if(top==-1)

{

printf("\nStack is Empty");

}

else

{

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

printf("\t%d",st[i]);

}

}

void main()

{

int dt,opt;

int q=0;

clrscr();

printf("This Program Is Used to Perform PUSH & POP operations On Stack");

printf("\n\n\tMain Menu.........");

printf("\n\n1.Push");

printf("\n\n2.Pop");

printf("\n\n3.Exit");

do

{

printf("\n\n\tEnter Your Choice 1-3:");

scanf("%d",&opt);

switch(opt)

{

case 1:

printf("\nEnter the Element to be Push:");

scanf("%d",&dt);

push(st,dt,tos);

if(flg==1)

{

printf("\nAfter Inserting the Element, Stack is:\n\n");

disp(st,tos);

if(tos==50-1)

printf("\nStack is Now Full");

}

else

printf("\nStack Overflow Insertion Not Possible");

break;

case 2:

dt=pop(st,tos);

if(flg==1)

{

printf("\n\tData Deleted From the Stack is:%d\n",dt);

printf("\n\tAfter Deleting the Element from the stack is:\n\n");

disp(st,tos);

}

else

printf("\nStack Empty,Deletio Not Possible:");

break;

case 3:

q=1;

break;

default:printf("\nWrong Choice Enter 1-3 Only");

}

}while(q!=1);

}

OUTPUT

Main Menu.........

1.push

2.pop

3.exit

Enter your choice 1-3:1

Enter the element to be push:4

After inserting the elements,stack is:

4

Enter your choice 1-3:1

Enter the element to be push:7

After inserting the elements,stack is:

7 4

Enter your choice 1-3:1

Enter the element to be push:4

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.