← Back to Home

Module 1: Stacks and Queues

Learning Objectives

Understanding Stacks

Stacks follow a Last-In-First-Out (LIFO) order, similar to a stack of blocks where you can only add or remove from the top. The most recently added item is always the first one you can remove.

Key operations include:

Real-world uses of stacks include:

Stack<String> history = new Stack<>();
history.push("edit document");
history.push("delete paragraph");
history.push("add image");

// Undo last action
String lastAction = history.pop(); // returns "add image"

Understanding Queues

Queues follow a First-In-First-Out (FIFO) order, similar to people waiting in line. The first item added to the queue will be the first one removed.

Key operations include:

Real-world uses of queues include:

Queue<PrintJob> printQueue = new LinkedList<>();
printQueue.add(new PrintJob("document1.pdf"));
printQueue.add(new PrintJob("photo.jpg"));
printQueue.add(new PrintJob("report.docx"));

// Process next job
PrintJob nextJob = printQueue.remove(); // returns document1.pdf job

Time and Space Complexity

Understanding the performance characteristics of stacks and queues is essential for efficient application design:

Stack Operations (using Java's Stack)

Queue Operations (using LinkedList implementation)

These constant-time operations make stacks and queues highly efficient for their specific use cases.

Resources

Stacks and Queues Code-Along Starter

Starter code for stacks and queues implementation.

Stacks and Queues Code-Along Solution

Solution code for stacks and queues implementation.

Stacks and Queues Examples

Additional examples of stacks and queues in practice.

Code-Alongs

Additional code-along exercises for this sprint.

Sprint Challenge

Access the sprint challenge for this unit.