01 / 20

LinkedList &
Queue Interface

Master FIFO patterns in Java using the Queue interface and LinkedList implementation: the backbone of orderly data processing.

AP Computer Science Data Structures

What Is a Queue?

A queue is a data structure that follows the First-In, First-Out (FIFO) principle: the first element added is the first one removed. Think of a line at a coffee shop: the person who arrives first gets served first.

FRONT (exit) BACK (enter) A B C D E poll() offer()

The Queue Interface in Java

Java provides a Queue interface in the java.util package. It defines the contract for FIFO behavior without dictating how it is implemented.

import java.util.Queue; import java.util.LinkedList; Queue<String> line = new LinkedList<>();

Why LinkedList for Queues?

A LinkedList is a chain of nodes, where each node holds a value and a reference to the next node. This structure makes adding to the tail and removing from the head both constant-time operations.

head tail "A" "B" "C" "D" "E" null Each node stores data + a pointer to the next node
Add/remove at ends: \( O(1) \) time

Core Queue Methods

offer(e)
Adds element e to the back of the queue. Returns true on success.
poll()
Removes and returns the front element. Returns null if the queue is empty.
peek()
Returns the front element without removing it. Returns null if empty.

Throws vs Returns Null

Operation Throws Exception Returns null / false
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

Queue vs Stack

Queue (FIFO) 1 2 3 4 5 out in Stack (LIFO) 5 4 3 2 1 in/out
Queue: FIFO
First in, first out. Like a line at a store.
Stack: LIFO
Last in, first out. Like a stack of plates.

How to Use a Queue: Step by Step

1
Import. Add import java.util.Queue; and import java.util.LinkedList; at the top of your file.
2
Declare. Use Queue<Type> as the variable type: Queue<String> q = new LinkedList<>();
3
Enqueue. Add elements with q.offer("item"). They go to the back.
4
Dequeue. Remove with q.poll(). The front element is returned.
5
Check. Use q.peek() to see the front, or q.isEmpty() before polling.

Example 1: Basic Queue Operations

Queue<String> q = new LinkedList<>(); q.offer("Alice"); // Queue: [Alice] q.offer("Bob"); // Queue: [Alice, Bob] q.offer("Carol"); // Queue: [Alice, Bob, Carol] System.out.println(q.peek()); // "Alice" System.out.println(q.poll()); // "Alice" (removed) System.out.println(q.poll()); // "Bob" (removed) System.out.println(q.peek()); // "Carol"

Example 2: Processing a Task Queue

Queue<String> tasks = new LinkedList<>(); tasks.offer("Print report"); tasks.offer("Send email"); tasks.offer("Update database"); while (!tasks.isEmpty()) { String task = tasks.poll(); System.out.println("Processing: " + task); }

Example 3: Customer Service Simulation

Queue<Integer> customers = new LinkedList<>(); // Customers arrive (ticket numbers) for (int i = 1; i <= 5; i++) { customers.offer(i); } // Serve first 3 customers for (int i = 0; i < 3; i++) { System.out.println("Serving #" + customers.poll()); } // Output: Serving #1, Serving #2, Serving #3 System.out.println("Next: #" + customers.peek()); // Output: Next: #4

Generics and Type Safety

The angle brackets <Type> enforce what kind of objects the queue can hold. This catches type errors at compile time instead of at runtime.

Queue<String>
Only String objects allowed. Calling offer(42) won't compile.
Queue<Integer>
Only Integer objects. Java auto-boxes int primitives.
Queue<MyObj>
Any class works. You define what types are valid.

Real-World FIFO Patterns

Print Queue
Documents print in the order they were submitted.
Web Server
HTTP requests processed in arrival order.
BFS Traversal
Breadth-first search explores graph nodes level by level using a queue.

Common Mistakes

✕ Using add() on full queue
add() throws an exception if the queue is capacity-bound and full. Use offer() instead: it returns false gracefully.
✕ Polling without checking
Calling poll() on an empty queue returns null. If you try to use the result without a null check, you get a NullPointerException.
✕ Declaring as LinkedList
Writing LinkedList<String> q = ... instead of Queue<String> q = ... exposes extra methods and breaks the interface contract.

Queue Methods: Summary

Method Action Returns If Empty?
offer(e) Add to back true N/A
poll() Remove front element null
peek() View front element null
isEmpty() Check size boolean true
size() Count items int 0

Question 1 of 5

What does FIFO stand for?

Question 2 of 5

What is printed?

Question 3 of 5

What does poll() return when called on an empty queue?

Question 4 of 5

Which declaration follows the best practice of programming to the interface?

Question 5 of 5

What is the value of sum after execution?