Javatpoint Logo
Javatpoint Logo

Implement Dynamic Deque using Templates Class and a Circular Array

A Deque, also known as a Double Ended Queue, is a type of Queue in which insertion and deletion are possible from both the front and back sides. A deque is a data structure that combines the capabilities of stacks and queues into a single data structure.

In this tutorial, we'll use the templates class and a circular array to create a dynamic deque.

Using a templates class and a circular array, create a dynamic Deque with the following functionalities:

  • front() returns the deque's first item.
  • size() returns the deque's element count.
  • back() returns the deque's final item.
  • capacity() returns the deque's current maximum number of elements.
  • push back(X): With push back, push X at the end of the deque (X).
  • empty(): Checks to see if the deque is empty.
  • push front(X): Use push front to push X at the start of the deque (X).
  • pop front(): Removes an element from the beginning of a deque.
  • Pop back(): Remove an element at the end of the deque.

The following is a step-by-step illustration:

  • The deque is initially empty.
  • Add 1 to the back of deque
  • Add elements 2 and 3 to the back of deque.
  • Add 4 to the front of deque.
  • Add 5 to the back of deque.
  • Select two elements from the front of the deque and two elements from the back.

Approach: When the array's capacity is reached, the idea is to double its size and copy the previous array's elements into the new array. Follow the steps below to resolve the issue:

  • To use the deque, set four variables: frontIndex, backIndex, sizeVar, and capacityVar, as well as an array, arr[].
  • Write a function called capacity() that returns the value of the variable capacityVar and determines the size of the current array in use.
  • Write a size() function that counts the elements in the deque and returns the value of the variable sizeVar.
  • Create a function called full() that checks whether the deque is full and returns true if sizeVar equals capacityVar. Otherwise, return false.
  • Write an empty() function that checks to see if the deque is empty and returns true if the frontIndex and backIndex are both -1. Otherwise, return false.
  • Make a function called Front() that prints the first element of the deque. If deque is not empty(), print the element at arr[frontIndex].
  • Create a Back() function to print the deque's last element. If deque is not empty(), print the element at arr[BackIndex].
  • Create a push back(X) function to insert an element at the end of the deque:
    1. If the deque is full, double the size of the current array and copy the elements of the previous array into the new array.
    2. If deque is empty(), set frontIndex and backIndex to 0, then set X to arr[frontIndex] and arr[backIndex], and increment sizeVar by one.
    3. If not, set backIndex to (backIndex+1)%capacityVar, assign X to arr[backIndex], and increment sizeVar by one.
  • Write a function called push front(X) that inserts an element at the start of the deque:
    1. If the deque is full, double the size of the current array and copy the elements of the previous array into the new array.
    2. If deque is empty(), set frontIndex and backIndex to 0, then set X to arr[frontIndex] and arr[backIndex], and increment sizeVar by one.
    3. If not, change frontIndex to (frontIndex-1 + capacityVar)%capacityVar, assign X to arr[frontIndex], and increment sizeVar by one.
  • Write a function called pop front() to delete an element at the start of the deque:
    1. If the deque is empty, print "Underflow".
    2. If sizeVar is greater than one, assign -1 to both frontIndex and backIndex, and decrement sizeVar by one.
    3. Otherwise, FrontIndex should be updated to frontIndex = (frontIndex+1)%capacityVar and sizeVar should be decremented by one.
  • Create a function called pop back() to delete an element at the beginning of the deque:
    1. Print "Underflow" if the deque is empty.
    2. Otherwise, if sizeVar is equal to 1, assign -1 to both frontIndex and backIndex and decrement sizeVar by one.
    3. Otherwise, decrement sizeVar by one and update backIndex as backIndex = (backndex-1 + capacityVar)%capacityVar.

Above Approach is implemented as follows:

C++ Program:

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 will be O(N).

Auxiliary Space will be O(N).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA