Computer Science
Grade 8
20 min
Lesson 7: Queues: First-In, First-Out (FIFO)
Introduce queues as a data structure that follows the FIFO principle.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a Queue is and explain its primary characteristic.
Identify real-world scenarios where Queues are used.
Describe the Enqueue and Dequeue operations.
Explain the First-In, First-Out (FIFO) principle.
Compare and contrast Queues with Stacks.
Trace the state of a Queue after a series of operations.
Conceptually describe how a Queue can be implemented using basic programming structures.
Ever waited in line for a roller coaster or for your turn at a video game? 🎢 That's a perfect example of a Queue in action!
In this lesson, we'll explore Queues, a fundamental data structure in computer science. You'll learn how they work, why they're important for organizing tasks, and how they follow a simple, fair rule: First-In, First-Ou...
2
Key Concepts & Vocabulary
TermDefinitionExample
QueueA linear data structure that stores a collection of items, where items are added at one end (the 'rear') and removed from the other end (the 'front').A line of people waiting to buy movie tickets.
FIFO (First-In, First-Out)The principle that the first item added to a Queue is always the first item to be removed. It's like a fair line where no one cuts.The person who arrived first in the movie ticket line is the first person to get their ticket.
EnqueueThe operation of adding an item to the rear (or 'tail') of a Queue.A new person joining the back of the movie ticket line.
DequeueThe operation of removing an item from the front (or 'head') of a Queue.The person at the front of the movie ticket line buying their ticket...
3
Core Syntax & Patterns
Enqueue Rule
Items are always added to the rear of the Queue.
To maintain the FIFO order, any new item joining the Queue must be placed at the end of the line, behind all existing items.
Dequeue Rule
Items are always removed from the front of the Queue.
The item that has been in the Queue the longest (the 'first in') is always the one that is removed first, ensuring the FIFO principle.
FIFO Principle
First In, First Out.
This is the defining characteristic of a Queue: the element that was added first will be the first one to be processed or removed. No cutting in line!
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A program executes this sequence: Enqueue(X), Enqueue(Y), Dequeue(), Enqueue(Z), Dequeue(), Dequeue(). For this sequence to run without causing an error, what must have been true about the queue's initial state?
A.It must have contained exactly one item.
B.It must have contained at least one item.
C.It must have been empty.
D.It must have contained at least two items.
Challenging
A web server processes requests from a queue. It takes 2 seconds to process one request. Requests arrive at time 0s (ReqA), 1s (ReqB), and 4s (ReqC). Which request is being processed at time 5s?
A.ReqA
B.ReqB
C.ReqC
D.No request is being processed.
Challenging
Imagine you want to simulate a Queue (FIFO) using only two Stacks (LIFO). To correctly Dequeue an item, what is the conceptual process?
A.Move all items from Stack1 to Stack2, Pop from Stack2, then move them all back to Stack1.
B.Pop an item from Stack1 and Push it onto Stack2.
C.Push the new item onto both Stack1 and Stack2 simultaneously.
D.It is impossible to simulate a Queue with two Stacks.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free