Last Updated: 18 January, 2025 5 Mins Read
The Deque stands for "Double Ended Queue".
Deque is an another type of queue which allows elements to be added or removed from both the front and the back which means we can insert and delete an element at the front end or rear end both.
Deque provides greater flexibility than a standard queue. This makes it particularly useful for scenarios where we need to access elements from both ends efficiently.
The following diagram represents a Deque in data structure.
Deque does not apply the concept of the FIFO (First-In-First-Out) so we can insert and delete elements from both ends of the queue.
There are mainly two types of Deque in data structure:
Let's discuss them in details.
Input restricted queue: It allows to perform the insert operation only at one end and the delete operation from both ends of the queue.
Let's see the diagram given below for understanding.
Output restricted queue: It allows to perform the delete operation only at one end and the insert operation from both ends of the queue.
Let's see the diagram given below for understanding.
In a Deque, we can perform the following operations, as given below:
Apart from the insert and delete operations, we can also perform the following operations on the deque data structure.
After understanding Deque with the help of the given definition and diagram, let's look at how it's implemented in Java.
Output
Insert element at REAR end: 10
insert element at REAR end: 20
get REAR element : 20
After delete, get REAR element: 10
Insert element at FRONT end: 30
Insert element at FRONT end: 40
get FRONT element: 40
After delete, FRONT element: 30
Operations | Time Complexity | Space Complexity |
---|---|---|
Insert Front | O(1) | O(n) |
Insert Rear | O(1) | O(n) |
Delete Front | O(1) | O(n) |
Delete Rear | O(1) | O(n) |
Search | O(n) | O(n) |
That's all, guys. I hope this article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com