VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implement-dynamic-deque-using-templates-class-and-a-circular-array/

⇱ Implement dynamic deque using templates class and a circular array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implement dynamic deque using templates class and a circular array

Last Updated : 23 Jul, 2025

The task is to implement a dynamic Deque using templates class and a circular array, having the following functionalities:  

  • front(): Get the front item from the deque.
  • back(): Get the last item from the deque.
  • push_back(X): Push X at the end of the deque.
  • push_front(X): Push X at the start of the deque.
  • pop_front(): Delete an element from the start of the deque.
  • pop_back(): Delete an element from the end of the deque
  • empty(): Checks whether deque is empty or not
  • capacity(): Current maximum number of elements deque can store
  • size(): Number of elements in the deque

Below is the step by step illustration:

  • Initially, the deque is empty
πŸ‘ Image
  • Insert 1 to the back of deque
πŸ‘ Image
  • Insert elements 2, 3 to the back of deque
πŸ‘ Image
  • Insert 4 to the front of deque
πŸ‘ Image
  • Insert 5 to the back of deque
πŸ‘ Image
  • Pop 2 elements from front and 2 elements from the back of deque
πŸ‘ Image
  • Pop 1 element from the front of deque
πŸ‘ Image

Approach: The idea is to double the size of the array used every time the capacity of the array gets full and copy the elements of the previous array into the new array. Follow the steps below to solve the problem:

  • Initialize 4 variables say frontIndex, backIndex, sizeVar, and capacityVar, and an array say arr[] to implement the deque.
  • Define a function say capacity() to find the size of the current array used and return the value of the variable, capacityVar.
  • Define a function say size() to find the count of elements in the deque and return the value of the variable, sizeVar.
  • Define a function say full() to find if the deque is full or not and return true if sizeVar is equal to capacityVar. Otherwise, return false.
  • Define a function say empty() to find if the deque is empty or not and return true if frontIndex and backIndex are equal to -1. Otherwise, return false.
  • Define a function say Front() to print the front element of the deque. If deque is not empty(), print the element of arr[frontIndex].
  • Define a function say Back() to print the last element of the deque. If deque is not empty(), print the element of arr[BackIndex].
  • Define a function say push_back(X) to insert an element at the end of the deque:
    • If the deque is full, then double the size of the current array and copy the elements of the previous array into the new array.
    • If deque is empty(), then assign frontIndex = backIndex = 0 and then assign X to both arr[frontIndex] and arr[backIndex] and then increment sizeVar by one.
    • Else, update backIndex as backIndex = (backIndex+1) %capacityVar and then assign X to arr[backIndex] and increment sizeVar by one.
  • Define a function say push_front(X) to insert an element at the start of the deque:
    • If the deque is full, then double the size of the current array and copy the elements of the previous array into the new array.
    • If deque is empty(), then assign frontIndex = backIndex = 0 and then assign X to both arr[frontIndex] and arr[backIndex] and then increment sizeVar by one.
    • Else, update frontIndex as frontIndex = (frontIndex-1 + capacityVar)%capacityVar and then assign X to arr[frontIndex] and increment sizeVar by one.
  • Define a function say pop_front() to delete an element at the front of the deque:
    • If the deque is empty, print β€œUnderflow”.
    • Else if sizeVar is equal to 1 then assign -1 to frontIndex and backIndex both and then decrement sizeVar by one.
    • Else, Update frontIndex as frontIndex = (frontIndex+1)%capacityVar and decrement sizeVar by one.
  • Define a function say pop_back() to delete an element at the front of the deque:
    • If the deque is empty, print β€œUnderflow”.
    • Else if sizeVar is equal to 1 then assign -1 to frontIndex and backIndex both and then decrement sizeVar by one.
    • Else, Update backIndex as backIndex = (backndex-1 + capacityVar) %capacityVar and decrement sizeVar by one.

Below is the implementation of the above approach:

 
 


Output
Current capacity 16
Current size 9
Front element 9
Rear element 8

Pop an element from front
Pop an element from back

Current capacity 16
Current size 7
Front element 7
Rear element 6


 

Time Complexity: O(N)
Auxiliary Space: O(N)


 

Comment