Operations on Deque:
Mainly the following four basic operations are performed on queue:
insertFront(): Adds an item at the front of Deque.
insertLast(): Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
In addition to above operations, following operations are also supported
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
Applications of Deque:
Since Deque supports both stack and queue operations, it can be used as both. The Deque data structure supports clockwise and anticlockwise rotations in O(1) time which can be useful in certain applications.
Also, the problems where elements need to be removed and or added both ends can be efficiently solved using Deque. For example see Maximum of all subarrays of size k problem., 0-1 BFS and Find the first circular tour that visits all petrol pumps.
See wiki page for another example of A-Steal job scheduling algorithm where Deque is used as deletions operation is required at both ends.
A Deque can be implemented either using a doubly linked list or circular array. In both implementation, we can implement all operations in O(1) time. We will soon be discussing C/C++ implementation of Deque Data structure.
Implementation of Deque using circular array
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- deque::emplace_front() and deque::emplace_back() in C++ STL
- deque::empty() and deque::size() in C++ STL
- deque::front() and deque::back() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- deque::clear() and deque::erase() in C++ STL
- deque::begin() and deque::end in C++ STL
- deque::at() and deque::swap() in C++ STL
- deque shrink_to_fit in C++ STL
- deque crbegin in C++ STL
- deque get_allocator in C++ STL
- deque crend in C++ STL
- deque cbegin() in C++ STL
- deque::push_front() in C++ STL
- deque::operator= and deque::operator in C++ STL
- deque::push_back() in C++ STL
- Deque vs Vector in C++ STL
- deque emplace in C++ STL
- Deque in Python
- deque insert() function in C++ STL
- deque rbegin() function in C++ STL
Improved By : TanmayS