The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

# Data Structures - Multiple Stack

When a stack is created using single array, we can not able to store large amount of data, thus this problem is rectified using more than one stack in the same array of sufficient array. This technique is called as Multiple Stack.

#### Note:

When an array of STACK[n] is used to represent two stacks, say Stack A and Stack B. Then the value of n is such that the combined size of both the Stack[A] and Stack[B] will never exceed n. Stack[A] will grow from left to right, whereas Stack[B] will grow in opposite direction ie) right to left.

## C program - Multiple Stack

Here is the program to demonstrate Multiple Stack.

multiple-stack.c
```#include <stdio.h>
#include <malloc.h>
#define MAX 10
int stack[MAX], topA = -1, topB = MAX;
void pushA(int val)
{
if(topA == topB-1)
printf("\n Overflow");
else
{
topA+=1;
stack[topA] = val;
}
}
int popA()
{
int val;
if(topA == -1)
{
printf("\n Underflow");
val = -999;
}
else
{
val = stack[topA];
topA--;
}
return val;
}
void display_stackA()
{
int i;
if(topA == -1)
printf("\n Stack A is empty");
else
{
for(i = topA;i >= 0;i--)
printf("\t %d",stack[i]);
}
}
void pushB(int val)
{
if(topB-1 == topA)
printf("\n Overflow");
else
{
topB-=1;
stack[topB] = val;
}
}
int popB()
{
int val;
if(topB == MAX)
{
printf("\n Underflow");
val = -999;
}
else
{
val = stack[topB];
topB++;
}
}
void display_stackB()
{
int i;
if(topB == MAX)
printf("\n Stack B is Empty");
else
{
for(i = topB; i < MAX;i++)
printf("\t %d",stack[i]);
}
}
int main()
{
int option, val;
do
{
printf("\n 1. PUSH a element into Stack A");
printf("\n 2. PUSH a element into Stack B");
printf("\n 3. POP a element from Stack A");
printf("\n 4. POP a element from Stack B");
printf("\n 5. Display the Stack A");
printf("\n 6. Display the Stack B");
printf("\n 7. Exit");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\n Enter the value to push on stack A :");
scanf("%d",&val);
pushA(val);
break;
case 2:
printf("\n Enter the value to push on stack B:");
scanf("%d", &val);
pushB(val);
break;
case 3:
if(val != -999)
printf("\n The value popped from Stack A = %d", val);
break;
case 4:
if(val != -999)
printf("\n The value popped from Stack B = %d",val);
break;
case 5:
printf("\n The contents of Stack A are :\n");
display_stackA();
break;
case 6:
printf("\n The contents of Stack B are :\n");
display_stackB();
break;
}
}while(option != 7);
return 0;
}```
```---------Menu---------
1. PUSH a element into Stack A
2. PUSH a element into Stack B
3. POP a element from Stack A
4. POP a element from Stack B
5. Display the Stack A
6. Display The Stack B
7. EXIT
Enter the value to push on stack A : 1

1. PUSH a element into Stack A
2. PUSH a element into Stack B
3. POP a element from Stack A
4. POP a element from Stack B
5. Display the Stack A
6. Display The Stack B
7. EXIT
Enter the value to push on stack A : 2

1. PUSH a element into Stack A
2. PUSH a element into Stack B
3. POP a element from Stack A
4. POP a element from Stack B
5. Display the Stack A
6. Display The Stack B
7. EXIT
The contents of Stack A are :
2 1

1. PUSH a element into Stack A
2. PUSH a element into Stack B
3. POP a element from Stack A
4. POP a element from Stack B
5. Display the Stack A
6. Display The Stack B
7. EXIT
The contents of Stack B are :
Stack B is Empty

1. PUSH a element into Stack A
2. PUSH a element into Stack B
3. POP a element from Stack A
4. POP a element from Stack B
5. Display the Stack A
6. Display The Stack B
7. EXIT
Enter your Choice : 7 ```

#### Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you