PROGRAM TO PERFORM POP OPERATION ON STACK USING ARRAY

/*PROGRAM TO PERFORM POP OPERATION ON STACK USING ARRAY*/
#include<conio.h>
#include<stdio.h>
#define MAX 5
void main()
{
int maxstk,top=-1,n,a[MAX],i;
char ch;
clrscr();
//PUSH OPERATION
printf(“Pushing elements into the stack: \n”);
do
{
if(top>=MAX-1)
{
printf(“Stack is overflow”);
break;
}
else
{
top++;
printf(“Enter element to be pushed: “);
scanf(“%d”,&n);
a[top]=n;
}
fflush(stdin);
printf(“\nDo you want to push more elements?? \npress y for yes otherwise n\t”);
scanf(“%c”,&ch);
}while(ch==’y’);
printf(“\nAfter push operation elements of the stack are:\n”);
for(i=0;i<=top;i++)
{
printf(“\t%d”,a[i]);
}
fflush(stdin);
printf(“\nDo you want to perform pop operation?? \npress y for yes otherwise n\t”);
scanf(“%c”,&ch);
if(ch==’y’)
{
//POP OPERATION
do
{
if(top<0)
{
printf(“Stack is underflow”);
break;
}
else
{
n=a[top];
printf(“\nElement poped is: “,n);
top–;
}
fflush(stdin);
printf(“\nDo you want to perform pop operation again?? \npress y for yes otherwise n\t”);
scanf(“%c”,&ch);
}while(ch==’y’);
if(top<0)
printf(“\nPress any key to exit\n”);
else
{
printf(“After pop operation elements of the stack are:\n”);
for(i=0;i<=top;i++)
{
printf(“\t%d”,a[i]);
}
}
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *