Stack and Queue Data Structures

Sanka Dissanayake
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.

  1. Polish notation
  2. reversing a string
  3. backtracking
  4. 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

  1. CPU scheduling
  2. Disk Scheduling
  3. When data is transferred asynchronously between two processes.
  4. Synchronization. eg: IO Buffers, pipes, file IO, etc.
  5. Handling of interrupts in real-time systems.
  6. 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 :

  1. Both stacks and queues provide the same output even their operations differ from each other.
  2. They both allow access to one element at a time, but they have reversed orders.
  3. In the end, both allow adding and removing elements in a particular order.
  4. According to the requirement, we can implement stacks using queues.
  5. Even though both stacks and queues are linear data structures and both store data sequentially, we can use both instead of another one.
  6. Insertion and Deletion operations can be performed on Stack as well as in queue.
  7. Both can be used to store homogeneous as well as heterogeneous (using structures ) data.
  8. Both have a front (input point) and a rear (output/exit point)
  9. Both are irregular Data Structures (as both work on reference unlike indexing in array)
  10. Both share very similar relations with Linked List
  11. Both can be implemented with other data structures such as HashMaps, Sets, ArrayList, Linked List, etc.
  12. Both can be implemented alternatively.
  13. A stack can be implemented as a queue and vice versa.

--

--