Queues and stacks are fundamental data structures in computer science, each with its unique characteristics. A queue follows the FirstIn, FirstOut (FIFO) principle, much like a reallife queue at a store, where the first person in line is the first to be served. A stack, on the other hand, adheres to the LastIn, FirstOut (LIFO) principle, similar to a stack of plates where the last plate placed on top is the first one to be removed.
While these data structures seem inherently different, it’s possible to simulate the behavior of a queue using two stacks. This technique proves valuable in scenarios where you might have access to a stack implementation but not a readily available queue implementation.
This comprehensive guide will delve into the intricacies of creating a queue using two stacks. We’ll explore the underlying concepts, algorithms, and stepbystep implementations, along with illustrative examples to solidify your understanding.
Understanding the Fundamentals
Before diving into the implementation, let’s recap the basic operations of queues and stacks:
Queue Operations:
 Enqueue: Adds an element to the rear of the queue.
 Dequeue: Removes an element from the front of the queue.
 Peek: Retrieves the element at the front of the queue without removing it.
 isEmpty: Checks if the queue is empty.
Stack Operations:
 Push: Adds an element to the top of the stack.
 Pop: Removes the element from the top of the stack.
 Peek: Retrieves the element at the top of the stack without removing it.
 isEmpty: Checks if the stack is empty.
The Core Concept
The key to simulating a queue with two stacks lies in utilizing the stacks to reverse the order of elements. Since a stack inherently reverses the order (LIFO), using two stacks allows us to reverse the order twice, effectively achieving the FIFO behavior of a queue.
We’ll explore two primary methods to achieve this:
 Method 1: Making Enqueue Operation Costly
 Method 2: Making Dequeue Operation Costly
Both methods achieve the same result but differ in their performance characteristics for enqueue and dequeue operations.
Method 1: Making Enqueue Operation Costly
In this method, we aim to keep the oldest element at the top of one of the stacks, making the dequeue operation straightforward.
Algorithm:

Enqueue(q, x):
 While
stack1
is not empty, push everything fromstack1
tostack2
.  Push
x
tostack1
.  Push everything back from
stack2
tostack1
.
 While

Dequeue(q):
 If
stack1
is empty, then error.  Pop the element from
stack1
and return it.
 If
Example:
Let’s say we want to enqueue the elements 1, 2, and 3 into our queue.
 Enqueue(1):
stack1
is empty, so we simply push 1 ontostack1
.  Enqueue(2): We move 1 from
stack1
tostack2
, push 2 ontostack1
, and then move 1 back fromstack2
tostack1
. Nowstack1
has 2 on top and 1 below it.  Enqueue(3): We move 2 and 1 from
stack1
tostack2
, push 3 ontostack1
, and then move 1 and 2 back fromstack2
tostack1
. Nowstack1
has 3 on top, followed by 2 and 1.
If we now perform a dequeue operation, we simply pop from stack1
, which gives us 1, the first element we enqueued.
Code Example (Python):
<responseelement_nghostngc418281173=”” ngversion=”0.0.0PLACEHOLDER”>
class Queue:
def __init__(self):
self.stack1 = []
self.stack2 = []
def enqueue(self, x):
while self.stack1:
self.stack2.append(self.stack1.pop())
self.stack1.append(x)
while self.stack2:
self.stack1.append(self.stack2.pop())
def dequeue(self):
if not self.stack1:
return None
return self.stack1.pop()
<divrole=”presentation” datamprt=”5″ style=”position: absolute; overflow: hidden; left: 0px; width: 5px; height: 5px;”><divstyle=”position: absolute; overflow: hidden; width: 1e+06px; height: 1e+06px; transform: translate3d(0px, 0px, 0px); contain: strict; top: 0px; left: 0px;”><divrole=”presentation” ariahidden=”true” style=”position: absolute; fontfamily: "Google Sans Mono", Consolas, "Courier New", monospace; fontweight: normal; fontsize: 14px; fontfeaturesettings: "liga" 0, "calt" 0; fontvariationsettings: normal; lineheight: 18px; letterspacing: 0px; height: 0px; width: 48px;”>