マツシタのお勉強

Use a single array to implement three stacks

Problem

Describe how you could use a single array to implement three stacks.

We can divide the array in three equal parts. So we use the array like this.

  • For stack 1, we will use [0, n/3)
  • For stack 2, we will use [n/3, 2n/3)
  • For stack 3, we will use [2n/3, n)

And with defining stackPointer which control the top position in each stack, we can control three stack in a array.