Set up the stage
Let's make it easier by using something concrete, say, poker cards.
Suppose you have four cards: A, 4, 6, 9, and you have two stacks.
As the name suggests, for the stacks, you can only put on a new card to the top of the stack, or take the top card off the stack.
To make it simple, let's keep the order (A, 4, 6, 9).
What we want is to make the box a queue.
That means when you take out cards from the box, they should keep the order. In other words, if 4 is latest card taken out, the next card (if any) would be 6.
Before we play, picture this box with two stacks inside (L and R).
For the stacks L and R, )
marks the top, where you put a card in or take a card out.
~~~~~~~~~~~~~~~~~~~~
| |
| [ L ) [ R ) |
|__________________|
Let the play begins
Ok let's play with the cards.
- put A in the box
- put 4 in the box
- take out a card from the box; it should be A
- put 6 in the box
- put 9 in the box
- take out a card from the box; it should be 4
- show me the next card in the box; it should be 6
- take out a card; it should be 6
- take out a card; it should be 9
Behind the scene
One can make it work like this: (for the above steps)
1. [A) [ )
2. [A 4) [ )
3a. [A) [4)
3b. [ ) [4 A)
3c. [ ) [4) ---> A
3d. [4) [ )
4. [4 6) [ )
4. [4 6 9) [ )
(... you got the idea ...)
Back to code
So ... what is different between this and your code?