A queue is a linear ADT that operates on the FIFO (First In, First Out) principle — the first item added is the first item removed. Like a real-world queue (e.g. people waiting at a checkout).
| Operation | Description | Condition |
|---|---|---|
| Enqueue | Add an item to the back of the queue | Fails if queue is full (overflow) |
| Dequeue | Remove an item from the front of the queue | Fails if queue is empty (underflow) |
| Peek / Front | View the front item without removing it | Fails if queue is empty |
| isEmpty() | Returns TRUE if the queue has no items | — |
| isFull() | Returns TRUE if the queue is at capacity | — |
A linear queue can be implemented using an array with two pointers:
// Linear queue using an array (size = 5) Initial state: front = 0, rear = 0, size = 0 // Enqueue "Alice": data[rear] = "Alice"; rear++; size++ // Enqueue "Bob": data[rear] = "Bob"; rear++; size++ // Dequeue: return data[front]; front++; size--
Problem with linear queue: After several enqueue/dequeue operations, the front pointer moves right, leaving wasted space at the beginning of the array that cannot be reused. This leads to a "false full" condition.
A circular queue solves the wasted space problem by treating the array as circular — when the rear pointer reaches the end of the array, it wraps around to position 0 (modulo arithmetic).
// Circular wrap-around: rear = (rear + 1) MOD maxSize front = (front + 1) MOD maxSize
This allows space freed by dequeuing to be reused by future enqueues.
A priority queue is a variant where items are dequeued in order of their priority rather than strictly FIFO. Higher priority items are removed first, regardless of when they arrived.
Applications: task scheduling, Dijkstra's algorithm, hospital triage systems.
| Application | Why a queue? |
|---|---|
| Print queue (printer spooler) | Documents print in the order submitted |
| CPU scheduling | Processes wait in order for CPU time |
| Keyboard input buffer | Keystrokes processed in order typed |
| Network packet handling | Packets processed in order received |
| Breadth-first search | Nodes visited in level order |
8 questions · instantly marked · AQA 7517 standard
| Term | Definition |
|---|
10 questions · 10 minutes