Skip to content
ComputerCS
Start learning

Computational Structures · Lesson 4.1.3

Queues: First In, First Out

Understand FIFO ordering, enqueue and dequeue operations, front and rear pointers, and the use of queues for scheduling and shared resources.

01

Learning objectives

  • Define a queue and explain First In, First Out ordering.
  • Trace enqueue, dequeue, front and is-empty operations.
  • Explain why circular queues reuse storage efficiently.
  • Identify real-world scheduling problems suited to queues.
02

Lesson notes

A queue is a linear structure in which insertion normally occurs at the rear and removal at the front. The first item added is the first one removed, so the queue follows First In, First Out (FIFO). It models fair waiting lines: a customer who arrived earlier should normally be served earlier.

Enqueue adds an item to the rear. Dequeue removes and returns the front item. Front or peek reads the next item without removing it. A queue can be implemented with a linked structure or a circular array; repeatedly deleting index zero from a Python list is simple to demonstrate but inefficient for a large queue because later elements must shift.

A circular queue treats the final position of an array as connected back to the first. Front and rear indices wrap around using modulo arithmetic. This reuses empty positions left by earlier dequeues, avoiding the wasted-space problem of a simple fixed array that only moves its rear pointer forward.

Queues are used in printer jobs, network packet handling, customer-support systems, CPU scheduling and breadth-first search. Different applications may use priority queues, where removal is based on priority rather than arrival order; that is a different scheduling policy from a standard FIFO queue.

03

Worked code examples

Worked example

deque provides efficient insertion at the rear and removal from the front, preserving FIFO order.

from collections import deque
queue = deque()
queue.append("Ali")
queue.append("Sara")
queue.append("Hina")
print("Next:", queue[0])
print("Served:", queue.popleft())
print("Waiting:", list(queue))
Output
Next: Ali
Served: Ali
Waiting: ['Sara', 'Hina']
04

Key terminology

Queue

A linear structure that normally removes the earliest inserted item first.

Enqueue

Add an item at the rear of a queue.

Dequeue

Remove and return the item at the front.

Circular queue

An array-based queue whose indices wrap around to reuse free positions.

05

Common misconceptions

Common mistake

A queue is LIFO.

Correct understanding

A standard queue is FIFO; LIFO describes a stack.

Common mistake

Every queue must sort items by their value.

Correct understanding

A normal queue preserves arrival order; a priority queue uses a different policy.

06

Check your understanding

1. Which operation adds an item to a queue?

2. Which item leaves a FIFO queue first?

3. Why use a circular queue?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.