Skip to content
BENIBIX Learn

Queue (Data Structure)

Development🧮 Algorithms & Data StructuresData Structures

Also known as Queue, FIFO, First In, First Out

A line where the first item in is the first one out. Join at the back.

Think of it like

Think of it like a line at a shop. The first person in line is the first one served.

Example

Emma and then Jack send pages to the office printer. The queue prints Emma's page first, because it arrived first.

Why it matters

Queues keep work fair and in order when it arrives faster than it can be done.

Definition

A queue is a list where items join at the back and leave from the front. The first item in is the first one out, which is called first in, first out. Adding is called enqueue and removing is called dequeue. Print jobs, messages and tasks waiting for a worker all sit in queues. In Python, a deque does both ends quickly.

Example code

python
>>> from collections import deque
>>> q = deque()
>>> q.append("Emma")
>>> q.append("Jack")
>>> q.popleft()
'Emma'
Emma joined first, so Emma leaves first.

Common mistake

Using a plain Python list and pop(0) as a queue. Each removal shifts every item; deque does it in one step.

Key takeaways

  1. 1
    First in, first out. Like a line at a shop.
  2. 2
    Back in, front out. Enqueue and dequeue.
  3. 3
    Keeps order. Fair for waiting work.

Compare