VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implementation-of-deque-using-array-simple/

⇱ Implementation of Deque using Array - Simple - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Deque using Array - Simple

Last Updated : 12 Mar, 2025

A Deque (Double-Ended Queue) is a data structure that allows insertion and deletion of elements at both ends (front and rear). This flexibility makes it more versatile than a regular queue, where insertion and deletion can only happen at one end. In this article, we will explore how to implement a deque using a simple array.

Key Features of a Deque

  • Insertions and deletions from both ends: You can add or remove elements from both ends of the deque.
  • Fixed size: The size of the deque is fixed when the array is created.
  • Efficient operations: Insertion and deletion from both ends are efficient if there is space available in the array.
👁 Deque-using-Array
Deque using Array

Basic Operations of a Deque Using a Simple Array

The key operations that can be performed on a deque implemented using a simple array:

1. Initialization

The deque is initialized using an array and two pointers, front and rear, which track the positions of the front and rear elements.

  • front and rear are initialized to -1 to indicate that the deque is empty.
  • The array arr will store the elements of the deque.

2. Insert at Front

To insert an element at the front of the deque, we shift all the elements one position to the right and place the new element at the front.

  • If the deque is empty, both the front and rear pointers are initialized to 0.
  • If the front pointer is at 0, there is no space for insertion at the front, and an error is returned.
  • If there is space, all elements are shifted to the right, creating room for the new element at the front.

3. Insert at Rear

To insert an element at the rear of the deque, we place it at the position of the rear pointer and increment the rear.

  • If the deque is empty, both front and rear are set to 0.
  • If there’s space, the element is inserted at the rear pointer and the rear is incremented.

4. Delete from Front

To delete an element from the front of the deque, we simply increment the front pointer to remove the element. If there’s only one element, both the front and rear are reset to -1.

  • If there is only one element, reset both front and rear to -1.
  • Otherwise, increment the front pointer to remove the element.

5. Delete from Rear

To delete an element from the rear of the deque, we decrement the rear pointer. If there’s only one element, reset both front and rear.

  • If there’s only one element, reset both front and rear.
  • Otherwise, decrement the rear pointer to remove the element.

6. Get Front and Rear

  • These functions return the elements at the front or rear of the deque.

7. Check if Empty or Full

  • isEmpty checks if the front pointer is -1, indicating that the deque is empty.
  • isFull checks if the rear pointer has reached the last index of the array.

Output
5 10 20 30 
10 20 
1 10 20 50 
OperationTime Complexity
insertFront(x)O(n)
insertRear(x)O(1)
deleteFront()O(n)
deleteRear()O(1)
getFront()O(1)
getRear()O(1)
isEmpty()O(1)
isFull()O(1)

Advantages and Limitations of Using a Simple Array for Deque

Advantages

  • Simple implementation: The array-based implementation is easy to understand and straightforward to code.

Limitations

  • Fixed size: The size of the array is fixed at the time of creation. Once the deque is full, no more elements can be inserted unless we resize the array manually.
  • Shifting elements: Inserting elements at the front requires shifting all other elements, which can be inefficient for large deques.
Comment
Article Tags:
Article Tags: