Data Structures
What Is a Queue in Data Structures?
A queue is a first-in, first-out (FIFO) data structure: items are added at the back and removed from the front, like a line at a counter.
Definition
A queue holds items in the order they arrive. The first item added is the first one removed, which is called first in, first out (FIFO).
Queues run print jobs, task schedulers, message brokers and breadth-first search.
How it works
The two main operations are enqueue (add to the back) and dequeue (remove from the front). Both should take O(1) time.
In Python, collections.deque gives O(1) operations at both ends. A plain list is slow for this, because removing the first item shifts every other item.
Variants change the removal rule. A priority queue always removes the item with the highest priority, and a circular buffer reuses one fixed block of memory, which suits streaming data such as audio or log lines.