codebytes: Manage multiple stacks using an ArrayList of Stack objects.
Q. Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous tack exceeds some threshold. Implement a data structure that minics this. This Data Structure should be composed of several stacks and should create a new stack once the previous one exceeds capacity. It's push() and pop() should behave identically to a single stack (that is, pop() should return the same values as it would if there were just a single stack).Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.
Approach:
1. Use an ArrayList to manage individual stacks.
2. When a stack gets full, create another one and push the value on to the new stack.
3. If a stack gets empty, remove it from the ArrayList.
Read full article from codebytes: Manage multiple stacks using an ArrayList of Stack objects.
No comments:
Post a Comment