Open In App

Difference between Queue and Deque (Queue vs. Deque)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Queue:

The queue is an abstract data type or linear data structure from which elements can be inserted at the rear(back) of the queue and elements can be deleted from the front(head) of the queue.

Queue Data structure

Queue Data structure

The operations allowed in the queue are:

  • insert an element at the rear
  • delete element from the front
  • get the last element
  • get the first element
  • check the size of the queue
  • check if the queue is empty or not

Deque:

The double-ended queue is an abstract data type that generalizes a queue from which elements can be inserted or deleted either from both front(head) or rear(tail) ends.

Deque Data structure

Deque Data structure

The operations allowed in deque are:

  • insert an element at the back
  • Insert an element at the front
  • delete the element at the back
  • delete the element from the front
  • get the last element
  • get the first element
  • check the size of the deque
  • check if the deque is empty or not

There are two types of deque:

  • Input restricted deque: An input restricted dequeue is a queue in which deletion can be done from both the ends but insertion from the front will not be allowed.                                                                                                                             
  • Output restricted deque: An output restricted dequeue is a queue in which insertion can be done from both the ends but deletion from the rear will not be allowed.

Difference between Queue and Deque:

S.no  

Queue

Deque

1. A queue is a linear data structure that stores a collection of elements, with operations to enqueue (add) elements at the back of the queue, and dequeue (remove) elements from the front of the queue. A deque (double-ended queue) is a linear data structure that stores a collection of elements, with operations to add and remove elements from both ends of the deque.
2. Elements can only be inserted at the end of the data structure. Elements can be inserted from both ends of the data structure.
3. Elements can only be removed from the front of the data structure. Elements can be removed from both ends of the data structure.
4. Queues are a specialized data structure that uses the FIFO approach i.e., First In First Out. Deque can be used to implement the functionalities of both Stack (LIFO approach i.e., Last In First Out) and Queue (FIFO approach i.e., First In First Out).
5. A queue can be implemented using Array or Linked List. Deque can be implemented using Circular Array or Doubly Linked List.
6. Queues can be used as a building block for implementing more complex data structures, such as priority queues or stacks. Deques can be used as a building block for implementing more complex data structures, such as double-ended priority queues or circular buffers.
7. Common queue operations include enqueue, dequeue, peek (return the front element without removing it), and size (return the number of elements in the queue). Common deque operations include addFirst (add an element to the front of the deque), addLast (add an element to the back of the deque), removeFirst (remove the first element from the deque), removeLast (remove the last element from the deque), peekFirst (return the first element without removing it), and peekLast (return the last element without removing it).
8. Queue elements cannot be accessed with the help of an iterator. Deque can be traversed using iterator.
9. Examples of queue-based algorithms include Breadth-First Search (BFS) and printing a binary tree level-by-level. Examples of deque-based algorithms include sliding window problems and maintaining a maximum or minimum value in a sliding window.

Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads