Back to Blogs
AT CSStacks and QueuesJune 1, 2026

Stacks and Queues: Choosing the Right Structure

How Advanced CS students can recognize LIFO and FIFO patterns in code tracing, simulations, and problem solving.

Stacks and queues are simple ideas with a lot of power. A stack removes the most recent item first. A queue removes the oldest item first. The hard part is not the definition; the hard part is recognizing which behavior the problem needs.

Stack: last in, first out

A stack is useful when the newest item should be handled first. Examples include undo history, matching parentheses, recursion tracing, and depth-first exploration.

Stack<String> history = new Stack<String>();
history.push("home");
history.push("profile");
System.out.println(history.pop());

This prints profile because it was added most recently.

Queue: first in, first out

A queue is useful when items should be handled in arrival order. Examples include waiting lines, print jobs, customer requests, and breadth-first exploration.

How to decide

  • If the problem says "most recent" or "backtrack," think stack.
  • If the problem says "in order received" or "fair waiting line," think queue.
  • If the problem explores neighbors level by level, think queue.
  • If the problem explores one path deeply before returning, think stack.

Practice idea

Trace the same sequence of values through a stack and a queue. Students quickly see that the operations may look similar, but the removal order changes everything.