All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Queues: a fixed-length list with enqueue and dequeue

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

A queue is first in, first out (FIFO): items join at the rear and leave from the front. You need Create, Enqueue, Dequeue, IsEmpty, IsFull and Size on a fixed-length list.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

How a queue works (spec 8.2.6)

Items are enqueued at the rear and dequeued from the front. Uses: print queues, keyboard buffers, processes waiting for the CPU, customers waiting for a service.

A circular queue in a fixed-length list

With a fixed list, keep a front index, a rear index and a count. Each index wraps round to 0 at the end using %, so freed slots are reused.

MAX_SIZE = 4
queue = [None] * MAX_SIZE
front = 0
rear = -1
count = 0

def is_empty():
    return count == 0

def is_full():
    return count == MAX_SIZE

def enqueue(item):
    global rear, count
    if is_full():
        print("Queue full")
    else:
        rear = (rear + 1) % MAX_SIZE
        queue[rear] = item
        count = count + 1

def dequeue():
    global front, count
    if is_empty():
        print("Queue empty")
        return None
    item = queue[front]
    front = (front + 1) % MAX_SIZE
    count = count - 1
    return item

for job in ["J1", "J2", "J3"]:
    enqueue(job)
print(dequeue(), dequeue())
enqueue("J4")
enqueue("J5")
print(queue, front, rear, count)

Output:

J1 J2
['J5', 'J2', 'J3', 'J4'] 2 0 3

Linear queues

A simpler queue uses a growing list: append() to enqueue and pop(0) to dequeue. This is fine for small data, but pop(0) shifts every item along, and a fixed-length linear queue without wrapping runs out of space at the end even when the front slots are free. That is why the circular version is used.

Worked example

A circular queue has MAX_SIZE 3, front 0, rear -1, count 0. Trace: enqueue A, enqueue B, dequeue, enqueue C, enqueue D. Give front, rear and count.

  1. enqueue A: rear 0, count 1
  2. enqueue B: rear 1, count 2
  3. dequeue returns A: front 1, count 1
  4. enqueue C: rear 2, count 2
  5. enqueue D: rear (2 + 1) % 3 = 0, count 3

Answer: front 1, rear 0, count 3; the queue is full and holds B, C, D

Key idea: FIFO: enqueue at the rear, dequeue from the front; in a fixed-length circular queue use (index + 1) % MAX_SIZE and a count to test empty and full.

Check yourself

  1. What is displayed when this code runs? ```python q = [] for n in [5, 1, 8]: q.append(n) first = q.pop(0) q.append(3) print(first, q) ```
    Show answer

    5 [1, 8, 3]. The first item in (5) is the first out; 3 joins at the rear: 5 [1, 8, 3].

  2. What is displayed when this code runs? ```python MAX_SIZE = 5 rear = 4 rear = (rear + 1) % MAX_SIZE print(rear) ```
    Show answer

    0. From the last index, (4 + 1) % 5 wraps round to 0.

  3. A circular queue of MAX_SIZE 6 starts empty. 5 items are enqueued, 3 dequeued and 2 more enqueued. What is its size (count)?
    Show answer

    4. count = 5 - 3 + 2 = 4.

  4. What is the value of rear after this code runs? ```python MAX_SIZE = 4 rear = -1 for i in range(6): rear = (rear + 1) % MAX_SIZE ```
    Show answer

    1. rear goes 0, 1, 2, 3, 0, 1, so it ends at 1 (assuming items were dequeued to make room).

  5. In a circular queue that keeps a count, how do you test whether it is full?
    • count == MAX_SIZE
    • rear == MAX_SIZE - 1
    • front == 0
    • rear == front
    Show answer

    count == MAX_SIZE. Because the indexes wrap, only the count reliably says whether every slot is used.

  6. Which situation is best modelled by a queue rather than a stack?
    • Undoing the last edits in a word processor
    • Reversing the letters of a word
    • Documents waiting to be printed in the order they were sent
    • Tracking the pages visited for a back button
    Show answer

    Documents waiting to be printed in the order they were sent. Printing is first come, first served (FIFO). The others need the most recent item first (LIFO).

  7. This dequeue function for a circular queue is missing a line. Which line completes it so that the output is 'A B'? ```python MAX_SIZE = 3 queue = ["A", "B", None] front = 0 count = 2 def dequeue(): global front, count item = queue[front] __________ count = count - 1 return item print(dequeue(), dequeue()) ```
    • `front = (front + 1) % MAX_SIZE`
    • `front = front - 1`
    • `front = MAX_SIZE % (front + 1)`
    • `front = (front + 1) // MAX_SIZE`
    Show answer

    `front = (front + 1) % MAX_SIZE`. front moves to the next slot, wrapping with %.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.