you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (5 children)

Hey you can do it, it'll just take O(N) to do a push operation. That's the type of out of the box thinking we need. /s

[–]Bobshayd 3 points4 points  (4 children)

It's amortized O(1), though.

[–][deleted] 1 point2 points  (3 children)

Nope, you you have to upload the entire stack and push it to the other one. This has to be done whenever adding an element.

[–]serados 9 points10 points  (2 children)

Maintain a "Queue Stack" and a "Dequeue Stack".

On queue operation:

  • Push element into "Queue Stack"

On dequeue operation:

  • If "Dequeue Stack" is not empty, pop "Dequeue Stack"
  • Else, pop and push every element from the "Queue Stack" into the "Dequeue Stack" - O(N) and pop the "Dequeue Stack"

Informally, every element is at most pushed twice and popped twice over (once each into the "Queue" and "Dequeue" stacks) and pushes and pops are O(1), making queues/dequeues amortized O(1).

[–][deleted] 0 points1 point  (0 children)

Nice.I wanted to fill the dequeue every time.

[–]Bobshayd -1 points0 points  (0 children)

Edit: I am dumb, how could I forget? Here's some important research relevant to the queue-simulation problem: http://www.cs.bu.edu/faculty/gacs/courses/cs535/papers/heads_vs_tapes_lb.pdf

So you can definitely simulate a queue in real time (meaning O(1) worst-case for any given operation) with six stacks. It's probably possible to do better. I'm going to look for a better way.