Computer Science Grade 8 20 min

Stacks and Queues

Stacks and Queues

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a Stack is and its Last-In, First-Out (LIFO) principle. Define what a Queue is and its First-In, First-Out (FIFO) principle. Compare and contrast the key differences between Stacks and Queues. Implement the basic operations of a Stack (push, pop, peek) using an array or list. Implement the basic operations of a Queue (enqueue, dequeue, peek) using an array or list. Trace the state of a Stack or Queue through a series of operations. Identify real-world scenarios where Stacks and Queues are used. Ever wonder how your browser's 'Back' button or the 'Undo' feature in your favorite editor works? 🤔 Let's find out how these simple but powerful data structures make it happen! In this lesson, we'll explore two spe...
2

Key Concepts & Vocabulary

TermDefinitionExample StackA data structure that stores items in a Last-In, First-Out (LIFO) order. Think of it like a stack of plates; you can only add a new plate to the top and take the top one off.A stack of cafeteria trays. The last tray placed on top is the first one you take. QueueA data structure that stores items in a First-In, First-Out (FIFO) order. It works just like a real-life line or queue.A line of students waiting for the school bus. The first student who got in line is the first one to get on the bus. LIFO (Last-In, First-Out)The principle that governs how a Stack works. The most recently added item is the first one to be removed.In a Pringles can, the last chip you put in (at the top) is the first one you eat. FIFO (First-In, First-Out)The principle that governs how a Q...
3

Core Syntax & Patterns

Stack Operations (LIFO) push(item) adds to the top. pop() removes from the top. peek() looks at the top. When using a list/array to build a stack, always add and remove items from the same end (usually the 'end' of the list) to maintain the Last-In, First-Out order. For example, use `list.append()` to push and `list.pop()` to pop. Queue Operations (FIFO) enqueue(item) adds to the back. dequeue() removes from the front. peek() looks at the front. When using a list/array for a queue, add items to one end (the 'back') and remove them from the other end (the 'front') to maintain the First-In, First-Out order. For example, use `list.append()` to enqueue and `list.pop(0)` to dequeue.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You are designing a system to manage tasks for a CPU. Tasks are assigned a priority, but for tasks with the same priority, the one that arrived first should be processed first. Which data structure is most appropriate for managing these same-priority tasks?
A.Queue, because it ensures fairness by processing tasks in the order they arrived (FIFO).
B.Stack, because the most recent task is likely the most important (LIFO).
C.Stack, because it is simpler to implement than a Queue.
D.Queue, because it allows you to remove tasks from the middle.
Challenging
Start with an empty stack and an empty queue. Perform the exact same sequence of operations on both: add(5), add(10), add(15), remove(), add(20). What are the final contents of the stack (bottom to top) and the queue (front to back)?
A.Stack: [5, 10, 20], Queue: [5, 10, 20]
B.Stack: [5, 20], Queue: [10, 15, 20]
C.Stack: [20, 10, 5], Queue: [20, 15, 10]
D.Stack: [5, 20], Queue: [5, 10, 15, 20]
Challenging
A stack currently contains [A, B, C] from bottom to top. After two operations, the stack contains [A, D]. Which of the following pairs of operations could have produced this result?
A.push('D'), pop()
B.pop(), pop()
C.push('D'), push('A')
D.pop(), pop(), push('D')

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Data Structures

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.