Implementation of Deque Using A Circular Array

Implementation of Deque using a circular array
Implementation of Deque using a circular array

To implement Deque employing a circular array we should track 2 pointer front and rear within the array, all the operations are on these 2 pointers.

The which means of circular array will be understood by the image below.

In this image the rear is behind the front, that’s once rear was at the top of the array and there have been some empty slots within the beginning of the array, thus the insertion of part at the rear would cause the rear to return at position zero, this can be as a result of the circular array is circular in nature.


Create an associate array of size n, wherever n is that the most size of Deque and initialize front and rear as -1, before any operation on the Deque. because the array is circular increment front or rear once they are a gift at the top of the array can take them to the start line and equally decrementing front and rear once they are at the start line can take them to the top of the array.

Learn one of the most powerful and portable programming languages C++ and become eligible to apply for the positions at Google, Microsoft, Facebook, Amazon etc.

insertFront(x)

  • If the array is full, the component can’t be inserted.
  • If there are not any components within the Deque(or array) it means the front is equal to  -1, increment front and rear, and set arr[front] as x.
  • Else decrement front and set arr[front] as x.

Time Complexity = O(1)

insertRear()

  • If the array is already full then it not possible to insert more elements.
  • If there are not any elements within the Deque, that is rear which is equal to -1, increase front and rear and set arr[rear] as x.
  • Else increment rear and set arr[rear] as x.

Time Complexity = O(1)

deleteFront()

blog banner 1
  • If the Deque is empty, return.
  • If there is only one element in the Deque, that is, front equals rear, set front and rear as -1.
  • Else increment front by 1.

Time Complexity = O(1)

deleteRear()

  • If the Deque is empty, return.
  • If there’s just one component within the Deque, that is, rear equals front, set front and rear as -1.
  • Else decrement rear by one.

Time Complexity = O(1)

getFront()

  • If the Deque is empty, return.
  • Else come arr[front].

Time Complexity = O(1)

getRear()

  • If the Deque is empty, return.
  • Else come arr[rear].

Time Complexity = O(1)

isEmpty()
If front is equals to -1 the Deque is empty, else it’s not.
Time Complexity = O(1)
isFull()
If (rear + 1) % n equals to the front then the Deque is full, else it’s not. Here n is that the most size of Deque.
Time Complexity = O(1)

Implementation of Deque using circular array in Java:

class ImplementationOfDequeUsingCircularArray {
// Maximum size of Deque
private static final int MAXIMUM SIZE = 100;
// Array to implement Deque
private static int deque[];
// Variables for representing the front and rear of dequeue
private static int front = -1;
private static int rear = -1;
private static void insertFront(int x) {
// if array is not full
if (!isFull()) {
// case 1 : If the array is empty of there is no element
// increase the front and rear and add element at arr[front]
if (front == -1) {
front = rear = 0;
deque[front] = x;
}
// else, decrement front circularly and add the
// new element at arr[front]
else {
if (front == 0) {
front = MAXIMUM SIZE - 1;
} else {
front--;
}
deque[front] = x;
}
}
}
private static void insertRear(int x) {
// if array is not full
if (!isFull()) {
// Then we have to check if we are inserting first element in the array
// Now increase the front and rear and add element at arr[rear]
if (rear == -1) {
front = rear = 0;
deque[rear] = x;
}
// else increment rear circularly and add
// new element at arr[rear]
else {
if (rear == MAXIMUM SIZE - 1) {
rear = 0;
} else {
rear++;
}
deque[rear] = x;
}
}
}
private static void deleteFront() {
// if array is not empty
if (!isEmpty()) {
// if there is only 1 element
// make front and rear as -1
if (front == rear) {
front = rear = -1;
}
// else increment front circularly
else {
if (front == MAXIMUM SIZE - 1) {
front = 0;
} else {
front++;
}
}
}
}
private static void deleteRear() {
// if array is not empty
if (!isEmpty()) {
// if there is only 1 element
// make front and rear as -1
if (front == rear) {
rear = front = -1;
}
// else decrement rear circularly
else {
if (rear == 0) {
rear = MAXIMUM SIZE - 1;
} else {
rear--;
}
}
}
}
private static int getFront() {
// if array is not empty return arr[front]
if (!isEmpty()) {
return deque[front];
}
return -1;
}
private static int getRear() {
// if array is not empty return arr[rear]
if (!isEmpty()) {
return deque[rear];
}
return -1;
}
private static boolean isEmpty() {
// if front is equal to -1 it means dequeue is empty
if (front == -1) {
return true;
}
return false;
}
private static boolean isFull() {
// if front element is 1 ahead of rear element then
// deque is full
if ((rear + 1) % MAXIMUM SIZE == front) {
return true;
}
return false;
}
public static void main(String[] args) {
deque = new int[MAXIMUM SIZE];
// Example
insertFront(5);
insertRear(10);
insertRear(11);
insertFront(19);
System.out.println(getFront());
System.out.println(getRear());
System.out.println(isFull());
deleteRear();
System.out.println(getRear());
deleteFront();
System.out.println(getFront());
System.out.println(isEmpty());
}
}

Time Complexity: As the per all the operations performed we have found that time complexity of all operations like insertfront(), insertlast(), deletefront(), deletelast()is O(1).

Implementation of Deque using circular array in C++

#include <iostream>

using namespace std;
// Maximum size of Deque
const int MAXIMUM SIZE = 100;
// Array to implement Deque
int deque[MAXIMUM SIZE];
//Variables for representing the front and rear of dequeue
int front = -1;
int rear = -1;
bool isEmpty() {
// if front is -1 it means there are not elements i.e. deque is empty
if (front == -1) {
return true;
}
return false;
}
bool isFull() {
// if front element is 1 ahead of rear element then
// deque is full
if ((rear + 1) % MAXIMUM SIZE == front) {
return true;
}
return false;
}
void insertFront(int x) {
// if array is not full
if (!isFull()) {
// case 1 : there are no elements
// increase the front and rear and add element at arr[front]
if (front == -1) {
front = rear = 0;
deque[front] = x;
}
// else, decrement front circularly and add the
// new element at arr[front]
else {
if (front == 0) {
front = MAXIMUM SIZE - 1;
} else {
front--;
}
deque[front] = x;
}
}
}
void insertRear(int x) {
// if array is not full
if (!isFull()) {
// if we are inserting for first time in the deque
// We have to increase the front and rear and add element at arr[rear]
if (rear == -1) {
front = rear = 0;
deque[rear] = x;
}
// else increment rear circularly and add
// new element at arr[rear]
else {
if (rear == MAXIMUM SIZE - 1) {
rear = 0;
} else {
rear++;
}
deque[rear] = x;
}
}
}
void deleteFront() {
// if array is not empty
if (!isEmpty()) {
// if there is only 1 element
// make front and rear as -1
if (front == rear) {
front = rear = -1;
}
// else increment front circularly
else {
if (front == MAXIMUM SIZE - 1) {
front = 0;
} else {
front++;
}
}
}
}
void deleteRear() {
// if array is not empty
if (!isEmpty()) {
// if there is only 1 element
// make front and rear as -1
if (front == rear) {
front = rear = -1;
}
// else decrement rear circularly
else {
if (rear == 0) {
rear = MAXIMUM SIZE - 1;
} else {
rear--;
}
}
}
}
int getFront() {
// if array is not empty return arr[front]
if (!isEmpty()) {
return deque[front];
}
return -1;
}
int getRear() {
// if array is not empty return arr[rear]
if (!isEmpty()) {
return deque[rear];
}
return -1;
}
int main() {
// Example
insertFront(5);
insertRear(10);
insertRear(11);
insertFront(19);
cout<<getFront()<<endl;
cout<<getRear()<<endl;
if (isFull()) {
cout<<"true"<<endl;
} else {
cout<<"false"<<endl;
}
deleteRear();
cout<<getRear()<<endl;
deleteFront();
cout<<getFront()<<endl;
if (isEmpty()) {
cout<<"true"<<endl;
} else {
cout<<"false"<<endl;
}
return 0;
}

Let’s try to solve a problem from LeetCode named “Next Greater Element II” which will give you a fair idea about circular array and how it actually works.

Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn’t exist, output -1 for this number.

Conquer the aptitude round of the interviews through this course. The series will enhance your Quantitative Aptitude, Logical Reasoning, Verbal Ability, and Data Interpretation skills. It will also ensure rigorous and comprehensive preparation with the assignments and mock tests.

Example 1:

Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1’s next greater number is 2;
The number 2 can’t find next greater number;
The second 1’s next greater number needs to search circularly, which is also 2.

Note: The length of given array won’t exceed 10000.

The first way to approach circular array problems is to extend the original array to twice of its length, 2nd half has the same element as first half. It will simplify the problem. First let’s think of a Naive approach, Just look for the next greater element directly. Time complexity: O(n^2).

public class Solution {
public int[] nextGreaterElements(int[] nums) {
int maximum = Integer.MIN_VALUE;
for (int num : nums) {
maximum = Math.max(maximum, num);
}
    int length = nums.length;
    int[] result = new int[length];
    int[] check = new int[length * 2];

    for (int i = 0; i < length * 2; i++) {
        check[i] = nums[i % length];
    }

    for (int i = 0; i < length; i++) {
        output[i] = -1;
        if (nums[i] == maximum) continue;

        for (int j = i + 1; j < length * 2; j++) {
            if (check[j] > nums[i]) {
                output[i] = check[j];
                break;
            }
        }
    }
    return output;
}

}

The second way is that we can use a stack. First, we have to push all indexes into the stack, smaller index on the top. Then we start from the end of the array finding for the first element (index) in the stack which is greater than the current one. That will surely to be the Next Greater Element. Then push the current element (index) into the stack.
Time complexity: O(n)

public class Solution {
public int[] nextGreaterElements(int[] nums) {
int length = nums.length;
int[] output = new int[length];
    Stack<Integer> stack = new Stack<>();
    for (int i = length - 1; i >= 0; i--) {
        stack.push(i);
    }

    for (int i = length - 1; i >= 0; i--) {
        output[i] = -1;
        while (!stack.isEmpty() && nums[stack.peek()] <= nums[i]) {
            stack.pop();
        }
        if (!stack.isEmpty()){
            output[i] = nums[stack.peek()];
        }
        stack.add(i);
    }
    return output;
}

}

To explore more on Data Structures, click here. Learn to write most efficient programmes and equip yourself to get solution for the complex codes. Get ready for the competitions like ACM-ICPC, Google Codejam and more. Companies like Google, Microsoft, Facebook, Amazon etc hire professionals with exceptional problem-solving skills.

Frequently Asked Questions

Is Deque a circular queue?

A deque is implemented as a circular array or a circular doubly linked list, but it is not the same as a circular queue.

How do I make an array circular in Java?

There are several methods to make an array circular, the simplest is that you can create another array of 2^n elements and add the elements from the original array twice in the new array. For accessing the elements from a particular index, start printing from that index and print n elements.

What is a Deque explain with an example?

Deque or a doubly ended queue is a queue that supports insert and delete at both ends. For example: null<-1<->3<->5<->7->null.

How is Deque implemented? What are the applications of Deque?

Deque is implemented using a circular array or a circular linked list. Deques are implemented in palindrome checkers and A-steal job scheduling algorithms.

What is full form Deque?

The full form of Deque is a doubly ended queue.

By Yogesh Kumar