B2.2.4 · Data structures
The queue (FIFO)
By the end you can
Trace enqueue, dequeue, peek, and circular queue wrapping using modulo pointer arithmetic.
A is a linear data structure that operates under theFirst-In, First-Out (FIFO) principle. Just like a queue of customers at a supermarket checkout, items are added at the rear (tail) and removed from the front (head).
Core operations and pointers#
- enqueue(item): Adds an item to the rear of the queue and advances the tail pointer.
- dequeue(): Removes and returns the item from the front and advances the head pointer.
- peek() / front(): Inspects the front item without removing it.
- isEmpty() / isFull(): Checks for empty state (head equals tail and count is 0) or full state (count equals capacity).
Why circular queues are necessary#
In a simple linear array queue, every dequeue() leaves empty slots at the beginning of the array. To reclaim that space, either every remaining item must be shifted forward ($O(n)$ expensive operation), or the queue eventually runs out of room even though slots at the front are empty!
A solves this elegantly by “wrapping around” to index 0 using modulo arithmetic:
// Modulo pointer wrapping in circular queues tail = (tail + 1) % CAPACITY; head = (head + 1) % CAPACITY;
Queue initialised with 2 items. FIFO order.
Check yourself
Queues
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 9 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Describe the FIFO principle of a queue and explain enqueue, dequeue, peek, and circular queue wrapping.
[4]Describe: A detailed account: say what happens, step by step if it is a process.
2.State three computer science applications of queue data structures.
[3]State: Give a short answer, no explanation needed.
3.Explain why a circular queue uses the modulo operator when updating the tail pointer during an enqueue operation.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
In a nutshell
- A queue is a linear data structure operating under the First-In, First-Out (FIFO) principle.
- Elements are inserted at the rear (enqueue) and removed from the front (dequeue).
- Circular queues use modulo pointer arithmetic (tail = (tail + 1) % capacity) to reuse empty front slots without memory shifting.
- Queues are used in print spoolers, CPU process ready queues, and network router packet buffers.
