Skip to content

Study guides & original practice

Stack versus queue practice

A stack removes the most recently added item first. A queue removes the earliest waiting item first. Choose the structure by the required access order.

By ExamCat · Original study lesson · Published September 24, 2026 · Updated November 24, 2026

Understand the method

The common names are LIFO for stacks and FIFO for queues. These describe behavior, not a specific programming-language implementation. Real systems may use priority queues or other policies, which are different from a simple FIFO queue.

Worked example, step by step

Add A, then B, then C to an initially empty stack and an initially empty queue.

  1. The stack top is C, so its first removal returns C.
  2. The queue front is A, so its first removal returns A.
  3. Removing all items gives C, B, A for the stack.
  4. Removing all items gives A, B, C for the queue. The insertion order was identical; the removal policy changed.

A mistake to watch for

A queue is not necessarily a priority ranking. In a priority queue, urgency or another key can override arrival order. State the policy before predicting which task runs next.

Try it yourself: three questions

Write an answer before opening the explanation. For calculations, keep your units and intermediate steps; for concepts, state the reason for your choice.

1. Push 1, push 2, pop, push 3, pop on a stack. What do the pops return?

Show answer and explanation

First 2, then 3. The value 1 remains after these operations.

2. Enqueue 1, enqueue 2, dequeue, enqueue 3, dequeue. What returns?

Show answer and explanation

First 1, then 2. The value 3 remains in the queue.

3. Why is an undo history often modeled with a stack?

Show answer and explanation

The latest action is usually the first one reversed. More complicated undo/redo behavior may use additional structures.

Trace undo and redo with two stacks

Consider a simple editor model with an undo stack U and a redo stack R. The rightmost entry is the top. A new action is pushed onto U and clears R. Undo pops U and pushes that action onto R; redo pops R and pushes it onto U. The editor also reverses or reapplies the action. This is a simplified history model, not a rule for every application.

Perform actions A, B and C, then undo twice and redo once. What history remains?

Follow each transfer
  1. After A, B, C: U = [A, B, C], R = [].
  2. First undo reverses C: U = [A, B], R = [C].
  3. Second undo reverses B: U = [A], R = [C, B].
  4. Redo reapplies B: U = [A, B], R = [C].

The latest undone action is the first redone. A FIFO queue would choose C before B and would not reproduce this model.

Now perform a new action D. Can you still redo C under these rules?

Check the new history branch

No. U becomes [A, B, D] and R becomes empty. Clearing the redo history prevents applying the abandoned action C after a different new action. If U is empty, undo must be rejected or treated as a no-op rather than popping a missing item.

For another stack application with a full trace, try postfix expression evaluation practice.

Build your next practice session

Mark the first step where your reasoning differed from the explanation. Turn that step into a question, answer it without looking, then change one condition in the example and explain what would change. A correct guess is a reason to revisit the method.

Use the free flashcard maker for your own question-and-answer pairs, or bring your course material into PDF-to-flashcards practice.

Further reading

OpenStax: data structures and algorithms provides textbook background. The worked scenario and practice questions on this page were written independently for ExamCat.

Related lessons

Browse this subject in the Blog ↗

Practice your own class material

Generate flashcards, notes and quizzes from your lecture notes in Exam Cat.

Try 7 days free ↗

Yearly plan: 7 days free, then $39.99/year. Cancel before the trial ends to avoid a charge.

APUSH study guide · Biology study guide · APUSH practice · All study tools