Artistry of Double-Ended Queue!. A brief analysis on how Deque can be…
By: Keerthana Thiagaraj
Tags: Data Structures, Java, Programming, Algorithms, Coding

A brief analysis on how Deque can be used over Stack Data structure.

We have numerous data structures to solve the problems by comparing the time complexity and space complexity. Recently, I came across a problem wherein I tried to use a data structure that yields a better performance over Stack. 🙇

Why you should not use Stack?🤔

By extending Vector , Stack provides operations that have no place in a stack, such as accessing elements via their index; inserting and deleting elements at any position. Also, extending other classes with unneeded functionality, bloat your objects, potentially costing a lot of extra memory and performance overhead.

, Stack provides operations that have no place in a stack, such as accessing elements via their index; inserting and deleting elements at any position. Also, extending other classes with unneeded functionality, bloat your objects, potentially costing a lot of extra memory and performance overhead. Stack does not implement an interface. Therefore, by using Stack, you commit yourself to a specific implementation.

Whereas, Deque exposes a set of operations that is all about being able to fetch/add/remove items from the start or end of a collection, iterate, etc and that's it. There's deliberately no way to access an element by position which makes Deque a consistent one! 🏋🏻

As deque is an interface, we need concrete classes to implement the functionality of the deque interface.

The two classes below, implement the deque interface.

ArrayDeque

LinkedList

An Application of Array Deque:

The following Java program checks whether the given string of Parentheses is valid or not, which helps to understand about deque. Here, I have used the ArrayDeque class to instantiate the deque interface.

(i.e) An input string is valid if:

1. Open brackets must be closed by the same type of brackets.

2. Open brackets must be closed in the correct order.

eg:- Input: s = “()”

Output: true

import java.util.ArrayDeque;

import java.util.Deque;

public class ValidParentheses {



public static void main(String[] args) {

String s = "([)]";

System.out.println("" + checkValidOrNot(s));

}



public static boolean checkValidOrNot(String s) {

// faster than using stack

Deque<Character> characterDeque = new ArrayDeque<>();



for (int i = 0; i < s.length(); i++) {

char bracket = s.charAt(i);



if (bracket == '(' || bracket == '{' || bracket == '['){

characterDeque.push(bracket);

continue;

}



if (characterDeque.isEmpty()) {

return false;

}

char checkBracket;

switch (bracket) {

case ')':

checkBracket = characterDeque.pop();

if (checkBracket == '{' || checkBracket == '[')

return false;

break;

case '}':

checkBracket = characterDeque.pop();

if (checkBracket == '(' || checkBracket == '[')

return false;

break;

case ']':

checkBracket = characterDeque.pop();

if (checkBracket == '{' || checkBracket == '(')

return false;

break;

}

}

return characterDeque.isEmpty();

}

}

Please refer here for the methods available in Deque.

Time Complexity:

Insertion or deletion in the middle is O(n).

The time complexity of random access by index is O(1).

It is O(1) for all enque(insert)/deque(delete) operations.

Points to be noted:✍🏻

Deque interface supports resizable arrays that can grow as required.

The ArrayDeque interface that implements the deque interface is memory efficient as it need not keep a track of the previous or next nodes. Thus deque is faster than the stack.🏇

Deque does not support concurrent access by more than one thread.

Deque is not thread-safe😐 unless an external synchronization is provided.

How and where it is used??

Deque permits LIFO behavior and thus it can be implemented as a stack though it is not a stack.

Also, you can use Deque as a queue (FIFO) by inserting elements at one end and removing elements at the other end.

It can also be used to implement priority queues.

The features of undo or history mostly present in the web browsers can be implemented using deques.

Thanks for reading💙

👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬