Skip to main content
added 33 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
~~~~~~~~~~~~~~~~~~~~
|                  |
| [  L  )  [  R  ) |
|__________________|
~~~~~~~~~~~~~~~~~~~~
|                  |
| [  L  )  [  R  ) |
|__________________|

Let the play beginsbegin

Behind the scenescenes

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 ...)
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 the code

So  ... what is the different between this and your code?

~~~~~~~~~~~~~~~~~~~~
|                  |
| [  L  )  [  R  ) |
|__________________|

Let the play begins

Behind the scene

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?

~~~~~~~~~~~~~~~~~~~~
|                  |
| [  L  )  [  R  ) |
|__________________|

Let the play begin

Behind the scenes

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 the code

So... what is the different between this and your code?

Source Link
XoXo
  • 185
  • 6

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.

  1. put A in the box
  2. put 4 in the box
  3. take out a card from the box; it should be A
  4. put 6 in the box
  5. put 9 in the box
  6. take out a card from the box; it should be 4
  7. show me the next card in the box; it should be 6
  8. take out a card; it should be 6
  9. 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?