// STACK
// Program 3 : Double Stack
/*

Stack A grows from left to right --->
+----+----+-..-+----+----+----+----+----+-..-+----+----+
| A1 | A2 | .. | An |    |    |    | Bm | .. | B2 | B1 |
+----+----+-..-+----+----+----+----+----+-..-+----+----+
                   <--- Stack B grows from right to left

Reference
Data Structures and Program Design in C++ - Kruse and Ryba
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define MAX 20

 int stack[MAX];
 int topA =  -1; // means stack A is empty
 int topB = MAX; // means stack B is empty

 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);
}
// end of main

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