Member-only story
Stack vs. Queue — How To, When To, and Why To Use Them
Everything you need to know about using the stack and queue data structures

Linear data structures are essential to software engineering and provide the foundations required to build the many features we use every day — think news feeds and notifications systems. When given the task to build these features, many engineers simply turn to arrays without considering two very important data structures: stacks and queues.
This article is a deep dive into the stack and queue data structures, their use cases, trade-offs and implementations. By the end of this article, you’ll have a clear understanding of stacks and queues to be able to use them in your own engineering tasks.
Table of Contents
- Why Should You Care About Stacks and Queues?
- What is a Stack?
- What is a Queue?
- Stack vs Queue — Use Cases
a) Stack use cases
b) Queue use cases - How to Implement a Stack and Queue
a) How to build a stack
b) How to build a queue - Stack vs Queue Summary
Why Should You Care About Stacks and Queues?
Before diving into the rabbit hole, it’s best to understand the reasons why you might want to consider a stack or queue rather than an array. As mentioned earlier a stack, queue and array are all linear data structures — however, the most performant operations differ between these data structures.
Three different notations can be used when comparing the performance of operations — Big O, Omega and Theta. Big O is more commonly used as it represents the worse case scenario.
By using contiguous memory storage, arrays execute random element access the fastest out of the three data structures — random access has a time complexity of O(1). The downside of using an array is that you can only achieve O(n) when searching, inserting and deleting elements. Alternatively, you’ll only achieve O(n) when using a stack or queue for random access, but insertions and deletions will have a time complexity of O(1).