Woodstock Blog

a tech blog for general algorithmic interview questions

[Question] Implement Queue Using Stacks

Question

link

Implement a queue by two stacks. Support O (1) push, pop, top.

Solution

Q.Push(x): S1-Push(x)

Q.Pop(): if S2.empty -> S1->S2 S2.pop()

Q.Top(): Similar with Q.Pop()

Learn and compare with another question [Question] Implement Stack using Two Queues.

Code

skipped