Data Structure Multiple Stack

A single stack is sometimes not sufficient to store a large amount of data. To overcome this problem, we can use multiple stack. For this, we have used a single array having more than one stack. The array is divided for multiple stacks.

Suppose there is an array STACK[n] divided into two stack STACK A and STACK B, where n = 10.

  • STACK A expands from the left to the right, i.e., from 0th element.
  • STACK B expands from the right to the left, i.e., from 10th element.
  • The combined size of both STACK A and STACK B never exceeds 10.

Multi Stack

Multi STACK Program in C

The following program demonstrates Multiple Stack -

#include <stdio.h>
#include <malloc.h>
#define MAX 10
int stack[MAX], topA = -1, topB = MAX;
void push_stackA(int val)
{
if(topA == topB-1)
printf("\n STACK OVERFLOW");
else
{
topA+=1;
stack[topA] = val;
}
}
int pop_stackA()
{
int val;
if(topA == -1)
{
printf("\n STACK UNDERFLOW");
}
else
{
val = stack[topA];
topA--;
}
return val;
}
void display_stackA()
{
int i;
if(topA == -1)
printf("\n Empty STACK A");
else
{
for(i = topA;i >= 0;i--)
printf("\t %d",stack[i]);
}
}
void push_stackB(int val)
{
if(topB-1 == topA)
printf("\n STACK OVERFLOW");
else
{
topB-=1;
stack[topB] = val;
}
}
int pop_stackB()
{
int val;
if(topB == MAX)
{
printf("\n STACK UNDERFLOW");
}
else
{
val = stack[topB];
topB++;
}
}
void display_stackB()
{
int i;
if(topB == MAX)
printf("\n Empty STACK B");
else
{
for(i = topB; i < MAX;i++)
printf("\t %d",stack[i]);
}
}
int main()
{
int option, val;
do
{
printf("\n -----Menu----- ");
printf("\n Enter 1 to PUSH a element into STACK A");
printf("\n Enter 2 to PUSH a element into STACK B");
printf("\n Enter 3 to POP a element from STACK A");
printf("\n Enter 4 to POP a element from STACK B");
printf("\n Enter 5 to display the STACK A");
printf("\n Enter 6 to display the STACK B");
printf("\n Press 7 to exit");
printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\n Enter a value to PUSH on STACK A :");
scanf("%d",&val);
push_stackA(val);
break;
case 2:
printf("\n Enter the value to PUSH on STACK B:");
scanf("%d", &val);
push_stackB(val);
break;
case 3:
printf("\n The value POPPED from STACK A = %d", val);
pop_stackA();
break;
case 4:
printf("\n The value POPPED from STACK B = %d", val);
pop_stackB();
break;
case 5:
printf("\n The STACK A elements are :\n");
display_stackA();
break;
case 6:
printf("\n The STACK B elements are :\n");
display_stackB();
break;
}
}while(option != 7);
return 0;
}

Output of the above program:


 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 1

 Enter a value to PUSH on STACK A :10

 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 1

 Enter a value to PUSH on STACK A :30

 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 1

 Enter a value to PUSH on STACK A :20

 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 5

 The STACK A elements are :
	 20	 30	 10
 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 3

 The value POPPED from STACK A = 20
 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 5

 The STACK A elements are :
	 30	 10
 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 2

 Enter the value to PUSH on STACK B:23

 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 2

 Enter the value to PUSH on STACK B:23

 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 6

 The STACK B elements are :
	 24	 23
 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 4

 The value POPPED from STACK B = 24
 -----Menu----- 
 Enter 1 to PUSH a element into STACK A
 Enter 2 to PUSH a element into STACK B
 Enter 3 to POP a element from STACK A
 Enter 4 to POP a element from STACK B
 Enter 5 to display the STACK A
 Enter 6 to display the STACK B
 Press 7 to exit
 Enter your choice: 






Read more articles


General Knowledge



Learn Popular Language