A Deque (Double-Ended Queue) is a linear data structure that allows elements to be added or removed from both ends—front and rear. Unlike a regular queue that adds and removes elements from one end (FIFO) or a stack that works by adding and removing elements from the top (LIFO), a deque allows you to add or remove elements from both ends, offering more flexibility.

It is widely used in various applications, including implementing algorithms like sliding window problems, and can be found in many programming languages' standard libraries.
Common Operations on Deque in Data Structure
1. push_front(value) : This operation adds an element at the beginning of the deque, shifting other elements to the right if necessary.
Example: If the deque is [2, 3, 4], after push_front(1), the deque becomes [1, 2, 3, 4].
2. push_back(value): This operation adds an element at the end of the deque without needing to shift other elements.
Example: If the deque is [1, 2, 3], after push_back(4), the deque becomes [1, 2, 3, 4].
3. pop_front(): This operation removes the first element of the deque, shifting the remaining elements to the left.
Example: If the deque is [1, 2, 3], after pop_front(), the deque becomes [2, 3].
4. pop_back(): This operation removes the last element from the deque without shifting any elements.
Example: If the deque is [1, 2, 3], after pop_back(), the deque becomes [1, 2].
5. front(): This operation gives the value of the first element of the deque, which is the element at the front.
Example: If the deque is [1, 2, 3], front() will return 1.
6. back(): This operation gives the value of the last element of the deque, which is the element at the back.
Example: If the deque is [1, 2, 3], back() will return 3.
7. empty(): This operation checks if there are any elements in the deque. It returns true if the deque is empty, and false otherwise.
Example: If the deque is [1, 2, 3], empty() will return false. If the deque is [], empty() will return true.
8. size(): This operation returns the number of elements present in the deque without modifying it.
Example: If the deque is [1, 2, 3], size() will return 3.
| Operation | Time Complexity |
|---|---|
| push_front | O(1) |
| push_back | O(1) |
| pop_front | O(1) |
| pop_back | O(1) |
| front | O(1) |
| back | O(1) |
| empty | O(1) |
| size | O(1) |
Other Operations on Deque in Data Structure
clear(): The clear() operation removes all elements from the deque, leaving it with a size of 0. This is useful when you want to reset a deque completely.
Example: If the deque is [1, 2, 3], after clear(), the deque becomes [].
erase(): The erase() function removes one or more elements from the deque. It takes an iterator pointing to the first element to be removed, and optionally, a second iterator specifying the position of the last element to be removed.
Example: If the deque is [1, 2, 3, 4], calling erase(pr.begin() + 1, pr.begin() + 3) will remove the elements at positions 1 and 2, resulting in [1, 4].
swap(): The swap() operation swaps the contents of one deque with another. This is an efficient way to exchange data between two deques.
Example: If deque A = [1, 2] and deque B = [3, 4], after A.swap(B), A = [3, 4] and B = [1, 2].
emplace_front(): The emplace_front() function inserts a new element at the front of the deque. Unlike the insert() operation, it avoids using the copy constructor of the element being inserted.
Example: If the deque is [2, 3, 4], after emplace_front(1), the deque becomes [1, 2, 3, 4].
emplace_back(): The emplace_back() function inserts a new element at the back of the deque, similar to emplace_front(). It avoids using the copy constructor of the element.
Example: If the deque is [1, 2, 3], after emplace_back(4), the deque becomes [1, 2, 3, 4].
resize(): The resize() function changes the number of elements in the deque to a specific value. If the new size is larger than the current size, new elements are added; if smaller, extra elements are removed.
Example: If the deque is [1, 2, 3], after resize(5, 0), the deque becomes [1, 2, 3, 0, 0].
assign(): The assign() function assigns new values to the elements in the deque, replacing its current contents with a new set of elements.
Example: If the deque is [1, 2, 3], after assign(3, 0), the deque becomes [0, 0, 0].
reverse(): The reverse() operation reverses the order of the elements in the deque, modifying its current arrangement.
Example: If the deque is [1, 2, 3], after reverse(), the deque becomes [3, 2, 1].
sort(): The sort() operation sorts the elements of the deque in ascending order using the less-than operator for comparisons.
Example: If the deque is [3, 1, 2], after sort(), the deque becomes [1, 2, 3].
| Operation | Time Complexity |
|---|---|
| clear() | O(n) |
| erase() | O(n) |
| swap() | O(1) |
| emplace_front() | O(1) |
| emplace_back() | O(1) |
| resize() | O(n) |
| assign() | O(n) |
| reverse() | O(n) |
| sort() | O(n log n) |