Data Structures · Visualization
Queue: Introduction
Queue
A queue is a collection where items are added at one end (the back) and removed from the other end (the front). Time: O(1) for enqueue/dequeue with a proper implementation (e.g. linked list or circular buffer). Space: O(n).
Big-O: enqueue / dequeue are both O(1).
Queue: Introduction
QueueA queue is a collection where items are added at one end (the back) and removed from the other end (the front). It follows the FIFO rule — First In, First Out — just like a line of people waiting: whoever joined the line first is the first one served.
How Queue: Introduction Works
- Keep track of both the 'front' (where items leave) and the 'back' (where items enter).
- To add an item, place it at the back (enqueue).
- To remove an item, take it from the front (dequeue).
- Optionally, peek at the front item without removing it.
Queue: Introduction Worked Example
Input: Enqueue 1, enqueue 2, enqueue 3, then dequeue
- Enqueue 1 → queue: [1]
- Enqueue 2 → queue: [1, 2]
- Enqueue 3 → queue: [1, 2, 3]
- Dequeue → removes 1 (first in) → queue: [2, 3]
Output: Dequeued value: 1, remaining queue: [2, 3]
Press ▶ Run to watch it step by step.
from collections import deque
q = deque()
q.append(1) # enqueue
q.append(2)
q.popleft() # dequeue
# FIFO: first in, first out
# example: qintro 1 on [1, 2, 3]Queue: Introduction Time & Space Complexity
When to Use Queue: Introduction
- Task scheduling and print job queues
- Breadth-first search (BFS) traversal of graphs and trees
- Handling requests in the order they arrive (e.g. web server request queues)
- Buffering data streams
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.