Course contentsShow
← Computer Science
Lesson 376 of 2,8729. Stacks and QueuesFree lesson

Essential Queue Operations

The fundamental operations: enqueue (add), dequeue (remove), and peek (view front element).

Essential Queue Operations

You'll learn the three core operations that bring any queue to life: enqueue, dequeue, and peek.

The Core Idea

A queue follows the FIFO (First-In-First-Out) principle, just like a line of people waiting for a bus. To make a queue work, we need three fundamental operations that let us interact with this data structure:

1. Enqueue (Add)

This operation adds a new element to the back (or rear) of the queue. Think of someone joining the end of a line at a ticket counter. The new element takes its place behind everyone else who arrived before it.

2. Dequeue (Remove)

This operation removes and returns the element at the front of the queue. It's like the person at the head of the line getting served and leaving. Whoever was second now becomes first. If the queue is empty, this operation typically signals an error or returns a special value.

3. Peek (View Front)

This operation lets you look at the element at the front of the queue without removing it. Imagine checking who's first in line without calling them forward. The queue remains unchanged—you're just taking a look.

Real-World Analogy

Picture a drive-through lane:

  • Enqueue: A new car joins the back of the line
  • Dequeue: The car at the window gets their order and drives away
  • Peek: The cashier looks to see which car is next without serving them yet

These three operations are all you need to manage the flow of data through a queue efficiently.

Key Takeaway: The essential queue operations—enqueue (add to back), dequeue (remove from front), and peek (view front)—define how elements enter, exit, and are observed in a FIFO structure.