Dequeue
A dequeue is a linear data structure, which stands for Double Ended Queue. Unlike queue,
where the data can be only inserted from one end and deleted from another, in a dequeue, the
data can be inserted and deleted from both front and rear ends.
The representation of a dequeue in data structure is represented below -
Types of dequeue in Data Structure
Basically, there are two types of dequeue in data structure
Input restricted queue
Output restricted queue
Let us discuss about them in detail.
Input restricted queue
In an input restricted queue, the data can be inserted from only one end, while it can be deleted
from both the ends.
Let's see a diagram to understand it more clearly.
Explanation: In the above example we can see how the data has been inserted from only one
end that is the front end, and how it has been deleted from both the ends that are front and
rear ends.
Explanation: In the above example we can see how the data has been inserted from only one
end that is the front end, and how it has been deleted from both the ends that are front and
rear ends.
Output restricted queue
In an Output restricted queue, the data can be deleted from only one end, while it can be inserted
from both the ends.
Let's see a diagram to understand it more clearly.
Explanation: In the above example, we can see how the data has been inserted from both the
ends(front and rear), and how it has been deleted from only one end(rear).
Operations on dequeue in data structure
In this section, we are going to implement a dequeue in data structure using the circular queue or
array. So before going further let's get a brief idea about what is a circular array.
Circular array: It is an array where the last element of the array is connected to the first
element. Thus, it forms a circle like structure, that makes it reusable for the empty spaces in the
previous indexes caused due to deletion operations.
In a circular array, if the array is full, we again start from the beginning. However in a linear
array, if the array is full, we can't insert elements anymore. In each of the operations explained
below, if the array is full, an "overflow message" will be thrown.
In a dequeue in data structure we can perform the following operations:
Insertion at front
Insertion at rear
Deletion at front
Deletion at rear
Before performing the following operations, we must follow the below steps :
Take a dequeue(array) of size n
Set two pointers variables front = -1 and rear = 0 at the first position.
Now, let us understand the operations performed on dequeue in data structure with examples.
Insertion at the front end
With the help of the Insertion at the front end operation we can insert the element from
the front end of the dequeue in data structure. There is a criterion before implementing the
operation, at first we need to check whether the dequeue is full or not. If the dequeue is not full,
then we can insert the element from the front end, by following the below conditions -
1. Initially, we will check the position of the front variable in our array
2. In case, the front variable is less than 1 (front < 1), we will reinitialize the front as the
last index of the array (front = n-1).
2. Otherwise, we will decrease the front by 1.
3. Add the new key for example 5 here into our array at the index front - array[front].
4. Every time we insert a new element inside the dequeue the size increases by 1.
Insertion at the rear end
With the help of the Insertion at the rear end operation, we can insert the element from
the rear end of the dequeue in data structure. We can insert the element from the rear end by
following the below conditions -
1. At first, we need to check whether the dequeue in data structure is full or not.
2. If the dequeue data structure is full, we have to reinitialize the rear with 0 (rear = 0).
3. Else increase the rear by 1.
4. Add the new key for example 5 here into our array at the index rear - array[rear].
5. Every time we insert a new element inside the dequeue the size increases by 1.
Deletion at the front end
With the help of the Deletion at the front end operation, we can delete the element from
the front end of the dequeue in data structure. We can delete the element from the front end by
following the below conditions -
1. At first, we need to check whether the dequeue in data structure is empty or not.
2. If the dequeue data structure is empty i.e. front = -1, we cannot perform the deletion
process and it will throw an error of underflow condition.
3. If the dequeue data structure contains only one element i.e. front = rear, set front = -
1 and rear = -1.
4. Else if the front is at the last index i.e. front = n - 1, we point the front to the starting
index of the dequeue data structure i.e. front = 0.
5. If none of the cases satisfy we simply increment our front by 1, front = front + 1.
6. Every time we delete any element from the dequeue data structure the size decreases by
1.
Deletion at the rear end
With the help of Deletion at the rear end operation, we can delete the element from the rear
end of the dequeue in data structure. We can delete the element from the rear end by following
the below conditions -
4. At first, we need to check whether the deque data structure is empty or not.
2. If the dequeue data structure is empty i.e. front = -1, we cannot perform the deletion
process and it will throw an error of underflow condition.
3. If the dequeue data structure contains only one element i.e. front = rear, set front = -
1 and rear = -1.
4. Else if the rear is at the starting index of the dequeue i.e. rear = 0, point the rear to the last
index of the dequeue data structure i.e. rear = n-1.
5. If none of the cases satisfy we simply decrement our rear by 1, rear = rear - 1.
6. Every time we delete any element from the dequeue the size decreases by 1.
we can also perform different peek operations as stated below :
To Get the front item from the dequeue in data structure
To Get the rear item from the dequeue in data structure
To check whether the dequeue data structure is full or not
To check whether the dequeue data structure is empty or not