This is the continuation of previous post on Array implementation of stack in java. In this post, we implements 2 stacks using one array.
While implementing stack using array we took one index which first initially point to -1, In this case of 2 stacks we take two index one point to -1 and other point to the end of the array i.e, size of the array.
- Start with two indexes one at the left and other at the right end of the array.
- Left index simulate the first stack and second index simulate the right stack.
- If we want to put the element in the first stack then put the element at the left index. Similarly, if we want to put the element in the second stackthen put the element at the right index.
- First stack grow towards left and second stack grow towards left.