Implementing multiple Stacks in a Single Array

Mrinal Walia
5 min readDec 13, 2018

To implement multiple stacks in a single array, one approach is to divide the array in k slots of size n/k each, and fix the slots for different stacks, we can use arr[0] to arr[n/k-1] for first stack, and arr[n/k] to arr[2n/k-1] for stack2 and so on where arr[] is the array of size n.

Although this method is easy to understand, but the problem with this method is inefficient use of array space.A stack push operation may result in stack overflow even if there is space available in arr[].

Examples:
Input :Enter the Number of stacks
Output :5
Input : 1.Push 2.pop 3.Display 4.exit
Input :Enter your choice
Output :1
Input :Enter the stack number
Output :2
Input :Enter element to push
Output :15
Input :Enter your choice
Output :2
Input :Enter the stack no to pop
Output :2
Output :15 from stack 2 is deleted
Input: Enter your choice
output: 3
output: Stack is empty
Input :Enter your choice
Output :4
Output :Program Terminated

Side Note

Programming language used for the article is written in python Some basis knowledge of python is required before understanding how things works in making predictions.

If you are new to python, I would…

--

--

Mrinal Walia
Mrinal Walia

Written by Mrinal Walia

Data Scientist and a Technical Writer! I will give you the best of Open-Source and AI.

Responses (1)