Community resourceWorksheet
OCR H446 1.4.1 Queues
Part 6 of 6 · H446 Data Structures and OOP
This worksheet introduces the concept of queues as a first in, first out data structure. Pupils explore how queues apply to real-life systems such as print job processing and practice implementing basic queue operations using head and tail pointers in Python. The tasks gradually build understanding from simple indexing to creating a fixed-capacity queue class, making it suitable for learners becoming confident with data structures and programming.
Shared by Chris H.Verified teacher
- 12 cells
- About 50 minutes
- CC BY-SA 4.0
- Shared 29 Jul 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Queues
OCR H446 references: 1.4.1(a), 1.4.1(b) and 2.2.1(f)
Many systems need to process work in the order it arrived. Songs requested for a student radio show, videos waiting to be rendered, print jobs sent to one printer and packets waiting for a network device all have an arrival order that matters.
A queue preserves that order. This worksheet develops the idea slowly before you trace pointers and implement a fixed-capacity linear queue in Python.
By the end, you should be able to:
- explain FIFO, enqueue and dequeue
- use head and tail pointers
- detect overflow and underflow
- compare a queue with a stack
- implement a queue without shifting every stored item
Process the oldest waiting item first
Imagine that four videos are submitted to a school media server for rendering:
interview.mp4, trailer.mp4, highlights.mp4, credits.mp4
If the server behaves fairly, interview.mp4 is processed first because it arrived first. New jobs join at the back; completed jobs leave from the front.
This is first in, first out, or FIFO.
Queue operations
enqueue(item)adds a new item at the back, also called the tail.dequeue()removes and returns the item at the front, also called the head.
After the four videos above are enqueued, one dequeue returns interview.mp4. The next dequeue returns trailer.mp4.
Error conditions
In a fixed-capacity implementation:
- overflow occurs when an enqueue is attempted but there is no available position
- underflow occurs when a dequeue is attempted but no items are waiting
The program should detect these cases before accessing the array.
Head and tail pointers
This worksheet uses a simple linear queue:
headis the index of the next item to removetailis the index of the next position in which to store an item
If head == tail, there are no active items waiting.
OCR-style pseudocode
function dequeue()
if head = tail then
return null
endif
value = queue[head]
head = head + 1
return value
endfunction
Python equivalent
def dequeue():
global head
if head == tail:
return None
value = queue[head]
head += 1
return value
Only the head pointer moves. The remaining items do not need to be shifted towards index zero.
A limitation of this simple version
Once tail reaches the end of the array, this linear queue cannot reuse positions before head, even if earlier items have been removed. A circular queue can reuse them, but circular implementation is not the main focus of this worksheet. Here, the aim is to understand FIFO behaviour and the two-pointer model clearly.
queue = [None, None, None, None]
head = 0
tail = 0
def enqueue(value):
global tail
if tail == len(queue):
return False
queue[tail] = value
tail += 1
return True
def dequeue():
global head
if head == tail:
return None
value = queue[head]
head += 1
return value
enqueue("A")
enqueue("B")
print(dequeue())
print(queue, head, tail)Ordering rule used by a queue: ______. Operation that adds at the tail: ______. Operation that removes at the head: ______. Pointer to the next item to remove: ______. Pointer to the next storage position: ______.- FIFO
- enqueue
- dequeue
- head
- tail
A linear queue has head = 1 and tail = 4. What should a successful dequeue do?
- ARemove position 3 and decrease tail
- BShift every item left and reset both pointers
- CReturn position 1 and increase head
- DStore a new item at position 4
Checkpoint: identify the active part of the queue
The physical array may still display an old value before head, but that value is no longer logically waiting. The active queue consists of the positions from index head up to, but not including, index tail.
For example, if head = 2 and tail = 5, the active items are at indices 2, 3 and 4. The next dequeue reads index 2; the next enqueue writes index 5.
Guided trace
Predict the service order before running the code. Record head, tail and the returned item after each operation. Then add one more arrival and one more service operation.
Independent processing task
Use a head index to process the supplied names in FIFO order:
- Start
headat zero. - While
headis less than the list length, appendwaiting[head]toserved. - Increase
head.
Do not use pop(0). It produces the correct order for a small Python list, but it shifts all remaining elements and hides the pointer behaviour this task is designed to practise.
waiting = ["Amina", "Noah", "Priya"]
head = 0
print(waiting[head])
head += 1
print(waiting[head])
# Predict the two printed names before running.
# Add "Leo" at the tail with append().
# Serve the remaining names by advancing head.
# Do not remove items with pop(0).waiting = ["Amina", "Noah", "Priya", "Leo"]
served = []
head = 0
# Process every name in FIFO order.
# Use head to access the next name and then advance it.
# Do not use pop(0).
Explain why a queue is suitable for print jobs sent to one printer. Include the ordering rule and the meaning of enqueue and dequeue in this scenario.
Apply all three terms to the printer rather than only defining them.
Students type their answer here.
Independent task: fixed-capacity linear Queue class
Complete a queue with a fixed list:
enqueue(value)stores attail, advancestailand returnsTrue.- If
tailhas reached the capacity, it returnsFalse. dequeue()returnsNonewhenhead == tail.- Otherwise it returns the head item and advances
head.
The supplied operations check normal FIFO behaviour, overflow and underflow. This is a linear queue, so positions before head are not reused.
class Queue:
def __init__(self, capacity):
self.items = [None] * capacity
self.head = 0
self.tail = 0
def enqueue(self, value):
pass
def dequeue(self):
pass
test_queue = Queue(3)
first_enqueue = test_queue.enqueue("A")
second_enqueue = test_queue.enqueue("B")
third_enqueue = test_queue.enqueue("C")
overflow_result = test_queue.enqueue("D")
first_dequeue = test_queue.dequeue()
second_dequeue = test_queue.dequeue()
third_dequeue = test_queue.dequeue()
underflow_result = test_queue.dequeue()State one difference between a stack and a queue. Then explain why advancing a queue's head pointer is preferable to shifting every remaining array item.
Name both ordering rules and give a linked efficiency explanation.
Students type their answer here.