Open In App

Introduction and Array Implementation of Deque

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Deque (Double-Ended Queue) is a data structure that allows elements to be added or removed from both ends—front and rear. Unlike a regular queue, which only allows insertions at the rear and deletions from the front, a deque provides flexibility by enabling both operations at either end. This makes it ideal for scenarios where you need fast access and modification of data from both sides, such as in scheduling, buffering, and palindrome checking etc.

Deques can be implemented using arrays or linked lists, and they support operations like insertFront, insertRear, deleteFront and deleteRear efficiently.

Key Features of Deque

  • Insertion at Both Ends: You can insert elements at both the front and the rear, providing more flexibility compared to a standard queue.
  • Deletion at Both Ends: Elements can be removed from either the front or the rear, making it useful for applications that require constant-time access to both ends of a collection.
  • Efficient Operations: Deques offer constant-time operations for adding and removing elements from both ends, which makes them highly efficient in scenarios requiring frequent modifications.
Deque-using-Array
Deque using Array

Basic Operations on a Deque

  1. insertFront(data): Adds an element to the front of the deque.
  2. insertRear(data): Adds an element to the rear of the deque.
  3. deleteFront(): Removes an element from the front of the deque.
  4. deleteRear(): Removes an element from the rear of the deque.
  5. getFront(): Retrieves the front element without removing it.
  6. getRear(): Retrieves the rear element without removing it.
  7. isEmpty(): Checks whether the deque is empty.
  8. size(): Returns the number of elements in the deque.
  9. erase(): Removes all elements from the deque

Simple Array Implementation of Deque

In a Simple Array Implementation of Deque, an array is used to store elements. The deque allows insertion and deletion from both ends, but the main challenge is managing the shifting of elements when performing operations. In this implementation, the front and rear pointers are used to track the insertion and deletion positions. The array has a fixed size, and resizing is required once the deque is full.

This approach can be inefficient because shifting elements can take linear time in some cases, and resizing the array can lead to additional overhead.

Key points:

  • Fixed size: The array has a predefined size, so resizing may be required.
  • Efficient for smaller deques: Performs well for smaller data sets or when the size of the deque is known.
  • Shifting elements: Elements need to be shifted when performing insertions and deletions, which may lead to inefficiency in certain cases.

Please Refer Array implementation of Deque – Simple for implementation

OperationsTime Complexity
insertFront(data)O(n) (due to shifting elements)
insertRear(data)O(1) (if no resizing)
deleteFront()O(n) (due to shifting elements)
deleteRear()O(1)
getFront()O(1)
getRear()O(1)
isEmpty()O(1)
size()O(1)
erase()O(n)

Circular Array Implementation of Deque

A Circular Array Implementation of Deque addresses the limitations of a simple array by allowing the array to "wrap around" when it reaches the end. Instead of shifting elements when performing insertions or deletions, the array's front and rear pointers "wrap around" to the beginning of the array, optimizing space usage.

This avoids the need for resizing and shifting, making operations more efficient. The circular array is implemented by treating the array as a circular buffer, which ensures that all positions in the array are used optimally.

Key points:

  • Circular nature: The array wraps around, allowing efficient usage of space.
  • No shifting: Insertion and deletion operations don’t require shifting of elements.
  • Fixed size with better space efficiency: The size is still fixed, but the circular design ensures no space is wasted.
  • Constant time operations: All operations (insertions and deletions) can be performed in constant time (O(1)).

Please Refer Implementation of Deque using circular array for implementation

OperationsTime Complexity
insertFront(data)O(1)
insertRear(data)O(1)
deleteFront()O(1)
deleteRear()O(1)
getFront()O(1)
getRear()O(1)
isEmpty()O(1)
size()O(1)
erase()O(n)

Next Article
Article Tags :
Practice Tags :

Similar Reads