How to implement a queue with three stacks?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Implementing a Queue with Three Stacks
Queues and stacks are fundamental data structures that provide essential functionality in computer science and software development. A queue follows a First-In-First-Out (FIFO) principle, where the first element added is the first to be removed. In contrast, a stack follows a Last-In-First-Out (LIFO) principle, where the last element added is the first to be removed. Despite their differences, it is possible to implement a queue using stacks. This article will guide you on how to implement a queue using three stacks.
Basic Concepts
Before diving into the implementation, let's briefly refresh our knowledge on stacks and queues:
- Stack: A collection that supports two primary operations: push (adding an element to the top) and pop (removing the top element).
- Queue: A collection that supports two primary operations: enqueue (adding an element to the back) and dequeue (removing the front element).
Why Use Three Stacks?
While it is possible to implement a queue using two stacks, introducing a third stack can simplify the process, especially in handling corner cases, and can result in cleaner, more maintainable code. The three stacks used in this implementation serve different roles:
- Input Stack: Used to handle incoming elements.
- Output Stack: Used for dequeue operations.
- Temp Stack: Helps with element transfer and order maintenance.
Implementation Details
Let's look at how to implement this:
Pseudocode for Queue Operations
- Enqueue OperationWhen an element is added to the queue via the
enqueueoperation, it is pushed onto the input stack.
- Dequeue OperationThe
dequeueoperation is slightly more complex:- If the output stack is empty, move all elements from the input stack to the output stack. This process maintains the FIFO order necessary for a queue. The temp stack can be used to correctly reverse the order of elements.
- Then pop the top of the output stack, which represents the front of the queue.
- Peek OperationSimilar logic applies to viewing the front element with the
peekoperation:
Key Points Summary
| Aspect | Description |
| Data Structures Used | Three Stacks: Input Stack, Output Stack, Temp Stack |
| Enqueue Complexity | - Element added to the input stack |
| Dequeue Complexity | - Elements transferred to maintain order (worst case when output stack is empty) Usually when output stack is pre-filled |
| Space Complexity | - Each element exists in only one stack at any time |
| Scalability | Effective for all basic queue operations with additional stack overhead |
Additional Considerations
- Memory Usage: This implementation uses additional memory compared to a two-stack solution but achieves a more straightforward logic path.
- Use Cases: Implementing queues with stacks is mostly educational but could be useful in cases where stack operations are inherently faster due to platform optimizations.
- Optimizations: When developing a production-grade solution, consider optimizing stack operations and balance memory usage.
Conclusion
Implementing a queue using three stacks provides a valuable exercise in algorithm design and showcases the versatility of common data structures. Although it involves more memory allocation than a direct queue implementation or a two-stack approach, it results in clearer code and may perform better in specific scenarios involving stack operation optimizations. Understanding this technique ensures a solid foundation for tackling complex data structure challenges in practice.
Related reading
- How to implement a repeating shuffle that's random - but not too random
- How to implement a tree data-structure in Java?
- How to implement an A algorithm?
- How to implement classic sorting algorithms in modern C?
- How to implement an image2D array sequence sliding window in tensorflow?
- How to implement an ordered, default dict?
- How to implement depth first search for graph with a non-recursive approach
- How to implement dfs using recursion?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.