Implement two stacks in an array
Algorithm:
- Take an array, for stack 1 push/pop from start of array and for stack 2 push/pop from end.
- Push to stack1
- If top1 + 1 >= top2 then
- throw Stack Full Exception
- array[top++] = item
- If top1 + 1 >= top2 then
- Push to Stack 2 (same as above)
- Pop from Stack1
- If top1 < 0 then
- throw Stack Empty Exception
- return array[top–]
- If top1 < 0 then
- Pop from Stack2 (same as above)
Latest Source Code:
Github: TwoStackInArray.java
Output:
1 2 3 4 5 6 7 8 9 101 102 4 5 6 7 8 9 Y node : Node(4)