Stack and Queue Data Structures
2 min readMay 12, 2021
What is a stack?
- Stack is a linear data structure that operates in a LIFO (Last In First Out) or FILO (First In Last Out) pattern.
- Stack is an abstract data type with a bounded (predefined) capacity.
- It allows adding and removing elements in a particular order.
Examples of the uses of stacks.
- Polish notation
- reversing a string
- backtracking
- quick sort algorithm
What is a queue?
- A queue supports the insert and removes operations using a FIFO (first-in-first-out) procedure or LILO (Last In Last Out) procedure.
- Queue also an abstract data type.
- Queue allows adding and removing elements in a particular order.
Examples of the uses of queues
- CPU scheduling
- Disk Scheduling
- When data is transferred asynchronously between two processes.
- Synchronization. eg: IO Buffers, pipes, file IO, etc.
- Handling of interrupts in real-time systems.
- Call Center phone systems use Queues to hold people calling them in an order
Can every problem solve by using the concept of a stack, also be solved by using a queue?
can be used. because :
- Both stacks and queues provide the same output even their operations differ from each other.
- They both allow access to one element at a time, but they have reversed orders.
- In the end, both allow adding and removing elements in a particular order.
- According to the requirement, we can implement stacks using queues.
- Even though both stacks and queues are linear data structures and both store data sequentially, we can use both instead of another one.
- Insertion and Deletion operations can be performed on Stack as well as in queue.
- Both can be used to store homogeneous as well as heterogeneous (using structures ) data.
- Both have a front (input point) and a rear (output/exit point)
- Both are irregular Data Structures (as both work on reference unlike indexing in array)
- Both share very similar relations with Linked List
- Both can be implemented with other data structures such as HashMaps, Sets, ArrayList, Linked List, etc.
- Both can be implemented alternatively.
- A stack can be implemented as a queue and vice versa.