IMPLEMENTATION OF
QUEUE USING STACK
P R E S E N TAT I O N BY J AYA D I T YA G U P TA
DATA
STRUCTURES
PRESENTATION:
2 N D
M AY 2 0 2 5
Add a Footer 2
INTRODUCTION:
What is a Queue?
A queue is a linear data structure that follows the FIFO principle — First In, First
Out. This means the element added first to the queue will be the first one to be
removed.
Enqueue – Add an element to the rear (end) of the queue.
Dequeue – Remove an element from the front of the queue.
What is a Stack?
A stack is a linear data structure that follows the LIFO principle — Last In, First Out.
This means the last element added to the stack is the first one to be removed.
Push – Add an element to the top of the stack.
Pop – Remove the element from the top of the stack.
Peek/Top – View the top element without removing it.
Add a Footer 3
WHY IMPLEMENT A QUEUE USING STACKS?
✅ 1. Understanding Abstract Data Types
It helps you deepen your understanding of how different data structures work and interact.
You learn to simulate one structure (queue) using another (stack), reinforcing logic and
problem-solving skills.
✅ 2. Stack Is Sometimes the Only Available Tool
In some systems or environments (e.g., limited memory or low-level systems), only stacks
might be available, so implementing a queue using them becomes necessary.
✅ 3. Foundation for Complex Structures
Used in building data stream processors, message queues, and task schedulers.
Helps in designing asynchronous systems and multi-threaded applications where stack and
queue interplay is useful.
Add a Footer 4
Methods of
TITLE Implementation:
Costly Enqueue (Using Costly Dequeue (Using
Two Stacks): Two Stacks):
We have two stacks and ……...... We use two stacks, called and
Where: ……….. to simulate a queue.
………… will be used to enqueue elements Dequeue (Remove):
(push).
If is empty:
……… will be used to dequeue elements
Pop all elements from and push
(pop).
them into
Enqueue (Insert):
This reverses the order (so the oldest is on
Simply push the element into
top).
This is fast (O(1) time).
Then pop from
This operation is costly — can take O(n)
time.
Code Example – Costly
Dequeue (Python)
Add a Footer 6
PSEUDOCODE – COSTLY DEQUEUE:
7
PSEUDOCODE – EFFICIENT DEQUEUE:
8
TITLE:
Time and Space
Complexity
Analysis
Add a Footer 9
USE CASES IN DSA: LIMITATIONS:
• Competitive Programming • Slower than native queue
implementation
• System Design (where only stack
access is available) • Higher memory usage
• Emulating queues in low-level • Transfer cost between stacks
APIs
• Used in browser history
(combined with another stack)
10
SUMMARY:
✅ Queue can be implemented using two stacks.
✅ Two popular methods: Costly dequeue & Efficient
dequeue
✅ Understand amortized complexity
✅ Common interview topic in DSA.
Add a Footer 11
THANK YOU
BY J AYA D I T YA G U P TA
Add a Footer 12