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

Deque: Double-Ended Queue Fundamentals

Understanding deques as a data structure that allows insertion and deletion from both front and rear ends.

Deque: Double-Ended Queue Fundamentals

What you'll learn: You'll understand what makes a deque unique—it lets you add and remove elements from both ends, unlike regular queues.

What is a Deque?

A deque (pronounced "deck," short for double-ended queue) is a data structure that combines the best of both stacks and queues. While a regular queue only lets you insert at the rear and remove from the front, a deque is more flexible: you can insert and delete from both the front and the rear.

Think of a deque like a line of people where newcomers can join at either end, and people can leave from either end. It's like having two doors instead of just one entrance and one exit.

Core Operations

A deque supports these fundamental operations:

  • Insert at front (push_front or addFirst)
  • Insert at rear (push_back or addLast)
  • Remove from front (pop_front or removeFirst)
  • Remove from rear (pop_back or removeLast)
  • Peek at front (view front element without removing)
  • Peek at rear (view rear element without removing)

Why Use a Deque?

Deques are powerful because they can act as:

  • A queue (use only rear insertion and front deletion)
  • A stack (use only one end for both operations)
  • Something unique (use all four operations as needed)

This flexibility makes deques useful for algorithms that need quick access to both ends, like sliding window problems or managing browser navigation (forward and back).

Key Takeaway: A deque is a double-ended queue that allows efficient insertion and deletion from both the front and rear, making it more versatile than standard stacks or queues.