Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Implement Stack using Queues
Bookmark
Input
Standard
Push Heavy
Alternating
Custom
operations
=
[push(1), push(2), push(3), pop(), top(), push(4), pop()]
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
empty
queue is empty
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
empty
queue is empty
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
front = top
back
enqueue, then rotate size - 1 times
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
2
front = top
back
2
is at the front, the stack top
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
2
3
front = top
back
enqueue
3
at the back
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
2
3
front = top
back
3
is at the front, the stack top
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
2
3
front = top
back
pop() → 3
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
2
front = top
back
enqueue, then rotate size - 1 times
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
4
2
front = top
back
dequeue
2
→ enqueue at the back
rotation 1/2
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
2
4
front = top
back
the front of the queue is the stack top
push(1)
push(2)
push(3)
pop()
top()
push(4)
pop()
0
1
2
1
1
2
2
front = top
back
all operations complete
algo
master
.
io
Step:
Start with an empty queue. The front of the queue will act as the stack top.
0 / 25
Input
Standard
Push Heavy
Alternating
Custom
operations
=
[push(1), push(2), push(3), pop(), top(), push(4), pop()]
0 / 25
algo
master
.
io
Step:
Start with an empty queue. The front of the queue will act as the stack top.