#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 20
int stack[MAX];
int topA = -1; int topB = MAX; int pushA(int );
int pushB(int );
int popA();
int popB();
void show();
main()
{
int x, opn;
do
{
printf("\n\n\t <1>PUSH A <2>PUSH B <3>POP A <4>POP B <5>SHOW <6>EXIT");
printf("\n\t Enter your option : ");
scanf("%d",&opn);
switch(opn)
{
case 1:
printf("\t Enter number to push : ");
scanf("%d", &x);
if( pushA(x))
printf("\n\t ERROR : OVERFLOW !!!");
printf("\t Press any key to continue ...");
getch();
break;
case 2:
printf("\t Enter number to push : ");
scanf("%d", &x);
if( pushB(x))
printf("\n\t ERROR : OVERFLOW !!!");
printf("\t Press any key to continue ...");
getch();
break;
case 3:
if( topA == -1)
printf("\n\t ERROR : UNDERFLOW !!!");
else
printf("\t poped number is : %d ", popA());
printf("\t Press any key to continue ...");
getch();
break;
case 4:
if( topB == MAX)
printf("\n\t ERROR : UNDERFLOW !!!");
else
printf("\t poped number is : %d ", popB());
printf("\t Press any key to continue ...");
getch();
break;
case 5:
show();
printf("\t Press any key to continue ...");
getch();
break;
}
}while( opn != 6);
}
int pushA(int x)
{
if( topA+1 == topB)
return -1;
topA ++;
stack[ topA] = x;
return 0;
}
int pushB(int x)
{
if( topA+1 == topB)
return -1;
topB --;
stack[ topB] = x;
return 0;
}
int popA ()
{
if( topA == -1)
return -1;
topA --;
return stack[ topA + 1];
}
int popB ()
{
if( topA == MAX)
return -1;
topB ++;
return stack[ topB - 1];
}
void show ( )
{
int i;
if( topA == -1)
printf("\n\t STACK A is empty.");
else{
printf("\n\t STACK A = [ ");
for( i=0; i<=topA; i++)
printf(" %d ", stack[i]);
printf("]");
}
if( topB == MAX)
printf("\n\t STACK B is empty.");
else{
printf("\n\t STACK B = [ ");
for( i=MAX-1; i>=topB; i--)
printf(" %d ", stack[i]);
printf("]");
}
}
Output