Skip to content
Computer Science · 10th Grade · Advanced Data Structures and Management · Weeks 10-18

Stacks and Queues: LIFO & FIFO

Students learn about abstract data types: stacks (Last-In, First-Out) and queues (First-In, First-Out), and their applications.

Common Core State StandardsCSTA: 3A-AP-14

About This Topic

Stacks and queues are two of the most widely used abstract data types in computing, and understanding them gives students a window into how software systems manage sequential work. A stack follows Last-In, First-Out (LIFO) order -- the most recently added item is the first to be removed -- while a queue follows First-In, First-Out (FIFO) order, like a line at a store. Both appear everywhere: call stacks in programming languages, undo/redo history in text editors, print queues, and task schedulers all rely on these structures. This topic aligns with CSTA standard 3A-AP-14.

Students often encounter stacks and queues through abstract diagrams before seeing why they exist. Connecting each structure to a concrete real-world system first -- then analyzing what property makes that system work correctly -- builds genuine understanding rather than rote memorization.

Active learning activities like physical simulations and role-playing make the pointer-based behavior of push/pop and enqueue/dequeue intuitive, which pays dividends when students later implement these structures in code.

Key Questions

  1. Differentiate between the LIFO and FIFO principles.
  2. Analyze real-world applications where stacks are essential.
  3. Construct a simple program utilizing a queue for task management.

Learning Objectives

  • Compare the operational principles of stacks (LIFO) and queues (FIFO) by identifying their distinct ordering mechanisms.
  • Analyze real-world scenarios to explain why a stack or a queue is the appropriate data structure for managing sequential operations.
  • Construct a basic program that simulates a task scheduler using a queue data structure.
  • Identify and describe at least three distinct applications of stacks in software development.
  • Evaluate the efficiency of using stacks and queues for specific problem-solving contexts.

Before You Start

Introduction to Data Types and Variables

Why: Students need a foundational understanding of how data is stored and manipulated in programming.

Basic Programming Constructs (Loops, Conditionals)

Why: Implementing stacks and queues often involves loops for iteration and conditionals for checking empty or full states.

Arrays or Lists

Why: Students should be familiar with sequential data structures like arrays or lists, as these are often used as the underlying implementation for stacks and queues.

Key Vocabulary

StackAn abstract data type that follows the Last-In, First-Out (LIFO) principle. Items are added (pushed) and removed (popped) from the same end, often called the 'top'.
QueueAn abstract data type that follows the First-In, First-Out (FIFO) principle. Items are added (enqueued) at one end (the 'rear') and removed (dequeued) from the other end (the 'front').
LIFOAcronym for Last-In, First-Out. The most recently added item to a data structure is the first one to be removed.
FIFOAcronym for First-In, First-Out. The first item added to a data structure is the first one to be removed.
PushThe operation of adding an element to a stack.
PopThe operation of removing the top element from a stack.
EnqueueThe operation of adding an element to the rear of a queue.
DequeueThe operation of removing an element from the front of a queue.

Watch Out for These Misconceptions

Common MisconceptionStacks and queues are just variations of arrays or lists.

What to Teach Instead

Stacks and queues are abstract data types defined by their access rules, not their storage mechanism. The key distinction is restricted access: stacks only allow insertion and removal at one end, queues at opposite ends. Physical simulations make it clear that the constraint is the point -- it is what makes these structures useful.

Common MisconceptionFIFO and LIFO only matter for theoretical exercises, not real programs.

What to Teach Instead

The operating system you are using right now depends on queues to schedule processes and stacks to manage function calls. Every time a function calls another function and returns, a call stack is being managed. Connecting these structures to running systems students already use makes the relevance immediate.

Active Learning Ideas

See all activities

Real-World Connections

  • Web browsers use a stack to manage navigation history. When you click the 'back' button, the browser 'pops' the current page off the stack to return to the previous one, demonstrating LIFO.
  • Print spoolers in operating systems utilize a queue to manage print jobs. Documents sent to the printer are added to the queue and printed in the order they were received, illustrating FIFO.
  • Text editors and word processors implement an undo feature using a stack. Each action is 'pushed' onto the stack, and when you undo, the last action is 'popped' and reversed, a clear LIFO application.

Assessment Ideas

Exit Ticket

Provide students with two scenarios: 1) A list of tasks to be completed in the order they arrive. 2) A web browser's back button functionality. Ask students to identify which scenario best represents a stack and which represents a queue, and to briefly explain their reasoning for each.

Quick Check

Present students with a sequence of operations for both a stack and a queue, for example: Stack: push(A), push(B), pop(), push(C). Queue: enqueue(X), enqueue(Y), dequeue(), enqueue(Z). Ask students to write down the state of the stack and queue after all operations are completed, verifying their understanding of push/pop and enqueue/dequeue.

Discussion Prompt

Facilitate a class discussion using the prompt: 'Imagine you are designing a system for managing customer support tickets. Would you use a stack or a queue? Justify your choice by explaining how the LIFO or FIFO principle applies to efficient customer service.' Encourage students to debate the merits of each approach.

Frequently Asked Questions

What is the difference between a stack and a queue in programming?
A stack removes items in reverse order of insertion (Last-In, First-Out), while a queue removes items in the same order they were added (First-In, First-Out). Think of a stack as a pile of plates and a queue as a line at a counter. Both structures restrict how you can access elements, which is what makes them useful for specific tasks.
Where are stacks used in real computer programs?
Stacks are used in function call management (every time a function is called, the return address is pushed onto the call stack), undo/redo systems in text editors, syntax parsing in compilers, and depth-first search in graph algorithms. The LIFO property ensures that the most recent context is always resolved first before returning to an earlier state.
Why would you choose a queue over a stack?
Use a queue when fairness and order matter -- when the first item added should be the first processed. Print spoolers, CPU task schedulers, network packet buffers, and customer service systems all require this property. A stack would process the most recently submitted job first, which is usually unfair in those scenarios.
How does active learning help students understand stacks and queues?
Physical simulations let students embody the data structure rather than just diagram it. When students act out push and pop operations or process a task queue as a team, the invariant behavior becomes memorable. It also makes errors immediately visible -- if someone exits from the wrong end of a queue, the group notices right away.