Programming Companion
Java

Programming technique · B2.2.4

Queues

A queue adds new items at the rear and handles existing items from the front. This preserves arrival order: the first item added is the first item available for removal.

IB DP CS standard B2.2.4: Explain queues as first in, first out structures, including enqueue, dequeue, front, isEmpty, qualitative effects on performance and memory, and suitability for a specific problem.
First in, first out (FIFO) Poster arrived first, so it is at the front. New print jobs join at the rear.
Poster
Map
Labels

The four required operations

enqueue

Add one item at the rear.

dequeue

Remove and return the front item.

front

Return the front item without removing it.

isEmpty

Check whether a front item exists.

Using Java's supplied queue operations

Java can represent a queue with the Queue interface backed by ArrayDeque. Here, offer, poll and peek correspond to enqueue, dequeue and front.

Using collections.deque as a queue

Python's collections.deque supports efficient operations at both ends. Use append() at the rear, popleft() at the front, and queue[0] to inspect the front.

Abstract operationJava callReturned valueQueue after operation, front firstStored count
enqueue Posteroffer()truePoster1
enqueue Mapoffer()truePoster, Map2
frontpeek()PosterPoster, Map2
dequeuepoll()PosterMap1
isEmptyisEmpty()falseMap1
Abstract operationPython callReturned valueQueue after operation, front firstStored count
enqueue Posterappend()Poster1
enqueue Mapappend()Poster, Map2
frontqueue[0]PosterPoster, Map2
dequeuepopleft()PosterMap1
isEmptylen(queue) == 0FalseMap1

Performance and memory, explained qualitatively

Use the two queue ends

Normal queue processing adds at the rear and removes at the front. Arbitrary middle removal is not the queue abstraction.

Stored memory changes

enqueue stores one more item and dequeue stores one fewer. front and isEmpty do not change the count.

Check before removal

An empty queue has no front item. Check first rather than removing or indexing a value that does not exist.

Suitability questionShould the oldest waiting item be handled first? If yes, a queue may be appropriate. Undo history is not a queue problem.
Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Queue Operation Trace

Challenge ID: PC-T12-C01 · Standards: B2.2.4

Collect the queue tracing sheet from the front of the class. Before running the supplied Java program, trace every operation by hand. Record the returned value, queue state from front to rear and stored-item count after each step. Hand the completed tracing sheet to your teacher, then run the program and correct any mistakes in a different colour. Explain why front leaves the queue unchanged. Do not implement a custom queue.

Scaffold available
Items A, B and C entering at the rear and leaving from the front while a queue trace changes step by step.

Print Queue

Challenge ID: PC-T12-C02 · Standards: B2.2.4, B2.3.2, B2.3.4

Use a Queue<String> for print-job names. Provide options to add a job at the rear, view the next job, print and remove the front job, and report when no jobs remain. Keep queue operations in clear methods and explain why first in, first out preserves ordinary arrival order. Do not add priority processing.

Scaffold available
Document jobs waiting in arrival order while the oldest job is sent to a printer first.

Customer Service Queue

Challenge ID: PC-T12-C03 · Standards: B2.2.4, B2.3.2, B2.3.3, B2.3.4

Use a queue to store customer names in arrival order. Allow customers to join, show who is next and serve the front customer. Track how many customers have been served and prevent removal from an empty queue. Include a short explanation of why a normal first in, first out queue is fair for this scenario.

Scaffold available
Customers waiting in arrival order with the front customer served first and a new customer joining at the rear.

Round-Robin Turn Queue

Challenge ID: PC-T12-C04 · Standards: B2.2.4, B2.3.2, B2.3.3

Use a queue of player names to manage repeated turns. Remove the player at the front, run one turn, then add that player to the rear when they remain active. Remove eliminated players permanently. Demonstrate several complete rotations and explain how dequeue followed by enqueue preserves the turn order.

Scaffold available
Players rotating through a round-robin queue while active players return to the rear and eliminated players leave.