# Dequeue (Double Ended Queue) Introduction!

A dequeue (short for "double-ended queue") is a data structure that allows for the insertion and removal of elements from both the front and the back of the queue. This makes it different from a standard queue, which only allows for insertion at the back and removal from the front. Dequeues are often implemented as a dynamic array or a linked list, and they can be used in a variety of applications, such as implementing a browser's forward and back buttons, or in certain algorithms that require the efficient insertion and deletion of elements at both ends of the queue.

Operations on dequeue:

**Insertion at the front:**Add an element at the front of the queue.**Insertion at rear:**Add an element at the end of the queue.**Deletion at the front:**Remove an element from the front of the queue.- Deletion at the rear: Remove an element from the end of the queue.
**get front:**returns the front element of the dequeue.**get rear:**returns the last element of the dequeue.

## How Dequeue Works?

A dequeue works by maintaining two pointers or indices, one for the front of the queue and one for the back of the queue. These pointers are used to keep track of the location of the first and last elements in the queue.

**Insertion at the front of the queue:**

- When inserting an element at the front of the queue, the front pointer is decremented by one and the new element is inserted at the location pointed to by the front pointer.

I**nsertion at the rear of the queue:**

- When inserting an element at the rear of the queue, the element is inserted at the location pointed to by the rear pointer, and the rear pointer is incremented by one.

**Deletion from the front of the queue:**

- When deleting an element from the front of the queue, the element is removed from the location pointed to by the front pointer, and the front pointer is incremented by one.

**Deletion from the rear of the queue:**

- When deleting an element from the rear of the queue, the rear pointer is decremented by one and the element at the location pointed to by the rear pointer is removed.

**Retrieving the front element:**

- The front element is the element located at the position pointed by the front pointer.

**Retrieving the rear element:**

- The rear element is the element located at the position pointed by the rear pointer.

Dequeue can be implemented using an array or a linked list. If implemented using an array, we need to be careful about overflow and underflow conditions and handle them accordingly.

## Types of Dequeue

There are two types of deque:

**Input Restricted Deque:**In this type of deque, either insertion can be performed at only one end or deletion can be performed at only one end.**Output Restricted Deque:**In this type of deque, either insertion can be performed at only one end or deletion can be performed at only one end.

### Input Restricted Deque

- In this type of deque, insertion operations can be performed at both ends, but deletion can only be performed at one end. This type of deque is also known as a "push-back" deque.

### Output Restricted Deque

- In this type of deque, deletion operations can be performed at both ends, but insertion can only be performed at one end. This type of deque is also known as a "pop-front" deque.

Both types of deque have different use cases. Input restricted deque are useful when elements are added to the queue from one end and removed from the other end, such as in a task queue. Output restricted deque are useful when elements are added to the queue from one end and removed from the same end, such as in a stack or a buffer.

In practice, most deque implementations allow for both insertion and deletion at both ends, making them essentially a combination of both input-restricted and output-restricted deque.

## Examples of Dequeue

```
class Deque:
def __init__(self):
self.items = []
def add_front(self, item):
self.items.insert(0, item)
def add_rear(self, item):
self.items.append(item)
def remove_front(self):
if not self.is_empty():
return self.items.pop(0)
def remove_rear(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
```

In this example, the deque is implemented using a Python list, with the add_front and add_rear methods for inserting elements, and the remove_front and remove_rear methods for deleting elements. The is_empty and size methods are used to check if the deque is empty and to get the number of elements in the deque, respectively.

## Applications of Dequeue

Dequeues have a variety of applications, some of which include:

**Browser history:**A deque can be used to implement the forward and back buttons in a web browser. This allows the user to move forward and backward through their browsing history.T

**ask schedulers:**A deque can be used to implement a task scheduler, where tasks are added to one end of the deque and removed from the other end as they are completed.**Palindrome checker:**A deque can be used to check if a word is a palindrome. Characters are added to the deque from one end and removed from the other end, allowing for easy comparison of the first and last characters.**Sliding window:**A deque can be used to implement a sliding window algorithm, where a window of fixed size is moved over a sequence of elements, and the elements within the window are maintained in a deque.**Graph algorithms:**A deque can be used in graph algorithms such as Breadth-First Search (BFS) and Depth-First Search (DFS) for traversing the graph.**Memory management:**A deque can be used to implement a memory management algorithm, where the least recently used pages are removed from one end of the deque and the most recently used pages are added to the other end.**In computer networking**, a deque can be used to implement flow control algorithms, where packets are removed from one end of the deque when they are transmitted and added to the other end when they are received.**In compilers**, a deque can be used to implement a syntax-directed translator, where the input string is scanned from left to right and the parse tree is constructed using a deque to hold the non-terminals.

## Time and Space Complexity of Dequeue

The time and space complexity of deque operations depend on the specific implementation of the deque.

**Insertion at front:**

- If implemented using an array, insertion at the front of the deque requires shifting all existing elements one position to the right, which has a time complexity of O(n) where n is the number of elements in the deque.
- If implemented using a linked list, insertion at the front of the deque has a time complexity of O(1).

**Insertion at rear:**

- If implemented using an array, insertion at the rear of the deque has a time complexity of O(1) if the array has enough capacity and O(n) if array needs to be resized.
- If implemented using a linked list, insertion at the rear of the deque has a time complexity of O(1).

**Deletion at front:**

- If implemented using an array, deletion at the front of the deque requires shifting all existing elements one position to the left, which has a time complexity of O(n) where n is the number of elements in the deque.
- If implemented using a linked list, deletion at the front of the deque has a time complexity of O(1).

**Deletion at rear:**

- If implemented using an array, deletion at the rear of the deque has a time complexity of O(1).
- If implemented using a linked list, deletion at the rear of the deque has a time complexity of O(1).

**get front:**

- If implemented using an array, get front of the deque has a time complexity of O(1).
- If implemented using a linked list, get front of the deque has a time complexity of O(1).

**get rear:**

- If implemented using an array, get rear of the deque has a time complexity of O(1).
- If implemented using a linked list, get rear of the deque has a time complexity of O(1).

The space complexity of a deque is determined by the number of elements it can hold at any given time.

- If implemented using an array, the space complexity is O(n) where n is the number of elements in the deque.
- If implemented using a linked list, the space complexity is O(n) where n is the number of elements in the deque, plus the space required to store the pointers in the linked list.

In general, deque implemented using array has a constant time complexity for insertion and deletion at the rear and O(n) time complexity for insertion and deletion at the front, while deque implemented using linked list has a constant time complexity for all the operations.

## Advantages and Disadvantages of Dequeue

### Advantages of using deque

- The ability to insert and remove elements from both ends of the deque allows for more flexibility in the types of algorithms and data structures that can be implemented using a deque.
- Insertion and deletion operations can be performed in O(1) time complexity, making deque more efficient than a standard queue or stack.
- A deque can be implemented using an array or a linked list, which allows for a choice of the best data structure to suit the specific needs of the application.

### Disadvantages of using deque

- Deques can use more memory than a standard queue or stack, as they need to maintain two pointers or indices for the front and rear of the deque.
- Deque implemented using an array may have the problem of overflow and underflow, which can be handled but it consumes extra time and space.
- In some cases, a standard queue or stack may be a more appropriate data structure for a given application, as they are simpler and have a more specialized use case.

### Conclusion

In conclusion, deque is a versatile data structure that can be used in a wide range of applications, and it is efficient in terms of time complexity. However, it may require more memory than a standard queue or stack, and it may not be the best choice for all types of applications.