The big idea: A queue is added to at one end and taken from at the other.
So the first item in is the first out — FIFO. Nobody who arrived later can get ahead.
Compare the two halves: the same items in, and the opposite order back out.
Interactive diagram
Explore the labelled diagram, charts and maps for this topic in full study mode.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
enqueue and dequeue
- enqueue(item) — join at the back
- dequeue() — leave from the front, and return
- Two different ends, which is the whole difference from a stack
- Both are O(1) when implemented properly
front and isEmpty
- front() — look at the next one out, without removing it
- isEmpty() — is anyone waiting?
- Check isEmpty before dequeuing
- Dequeuing an empty queue is an error
What makes it fair
- Order of arrival is preserved exactly
- Nothing can jump ahead
- That fairness is why it is used for shared resources
from collections import deque
queue = deque()
queue.append("Ana") # enqueue — join the back
queue.append("Ben") # enqueue
print(queue[0]) # front → Ana
print(len(queue) == 0) # isEmpty → False
next_up = queue.popleft() # dequeue → Ana
print(queue) # deque(['Ben'])Queue<String> queue = new LinkedList<>();
queue.add("Ana"); // enqueue — join the back
queue.add("Ben"); // enqueue
System.out.println(queue.peek()); // front → Ana
System.out.println(queue.isEmpty()); // isEmpty — false
String nextUp = queue.remove(); // dequeue → Ana
System.out.println(queue); // [Ben]Why not a plain list: Removing from the front of an ordinary list means shifting every remaining item down one place — O(n) every time.
A proper queue structure removes from the front in constant time, which is why Python offers deque for the job.
Get feedback like a real examiner
Submit your answers and get instant feedback — what you did well, what's missing, and exactly what to write to score full marks.
Print jobs
One printer, many people. Jobs are printed in the order they were sent.
A stack here would print the most recent job first, leaving the person who sent theirs an hour ago waiting indefinitely.
Processes waiting for the CPU
Round-robin scheduling is a queue: take the front process, give it a time slice, put it at the back.
That is exactly the shape of the scheduling topic in Unit A1.
Buffering data
Data arriving faster than it can be handled waits in a queue.
Keystrokes, network packets and streamed audio all queue, and all must be handled in the order they arrived to make sense.
Breadth-first exploration
Exploring everything one step away before anything two steps away means queuing what you find.
This is how a shortest-path search over equal steps works.
Stack or queue, in one question: Does the most recent item matter most, or the one that has waited longest?
Most recent → stack. Longest waiting → queue. Every question here is that question in disguise.
How this is tested — you must justify FIFO from the scenario and say what a stack would get wrong. It comes up two ways:
Paper 2 — working with code
- Explain a queue and its operations, 3-5 marks
- Trace a sequence of enqueues and dequeues
- Choose between a stack and a queue
Paper 2 — the algorithmic-thinking question
- Decide from described behaviour, with no code shown
- Explain the consequence of choosing the wrong one
The classic trap: Describing a queue as "a list that things are added to". The mark is for the two ends: joining at the back and leaving from the front, so order of arrival is preserved.
A hospital reception system handles patients arriving for a walk-in clinic. Explain why a queue suits this, and what would happen with a stack.
Model answer plan
See the answer broken into clear points, with guidance on how each point earns marks.