The Wayback Machine - https://web.archive.org/web/20240822105435/https://www.geeksforgeeks.org/deque-set-1-introduction-applications/
Open In App

Deque | Set 1 (Introduction and Applications)

Last Updated : 18 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.

Operations on Deque: Below is a table showing some basic operations along with their time complexity, performed on deques.

Operation

Description

Time Complexity

push_front()

Inserts the element at the beginning.

O(1)

push_back()

Adds element at the end.

O(1)

pop_front()

Removes the first element from the deque.

O(1)

pop_back()

Removes the last element from the deque.

O(1)

front() Gets the front element from the deque.

O(1)

back() Gets the last element from the deque.

O(1)

empty() Checks whether the deque is empty or not.

O(1)

size() Determines the number of elements in the deque.

O(1)

Other operations performed on deques are explained as follows:
clear(): Remove all the elements from the deque. It leaves the deque with a size of 0.
erase(): Remove one or more elements from the deque. It takes an iterator specifying the position of the first element to be removed, and an optional second iterator specifying the position of the last element to be removed.
swap(): Swap the contents of one deque with another deque.
emplace_front(): Insert a new element at the front of the deque. It is similar to the insert operation, but it avoids the copy constructor of the element being inserted.
emplace_back(): Insert a new element at the back of the deque. It is similar to the insert operation, but it avoids the copy constructor of the element being inserted.
resize(): Change the number of elements in the deque to a specific number. If the new size is larger than the current size, new elements are appended to the deque. If the new size is smaller than the current size, elements are removed from the deque.
assign(): Assign new values to the elements in the deque. It replaces the current contents of the deque with new elements.
reverse(): Reverse the order of the elements in the deque.
sort(): Sort the elements in the deque in ascending order. It uses the less-than operator to compare the elements.

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 to both ends can be efficiently solved using Deque. For example see the Maximum of all subarrays of size k problem., 0-1 BFS, and Find the first circular tour that visits all petrol pumps. See the wiki page for another example of the A-Steal job scheduling algorithm where Deque is used as deletions operation is required at both ends. 

Some Practical Applications of Deque:

  • Applied as both stack and queue, as it supports both operations.
  • Storing a web browser’s history.
  • Storing a software application’s list of undo operations.
  • Job scheduling algorithm

Monotonic Deque : 

  • It is deque which stores elements in strictly increasing order or in strictly decreasing order 
  • To maintain monotonicity, we need to delete elements
    • For example – Consider monotonic(decreasing) deque dq =  {5, 4, 2, 1} 
    • Insert 3 into dq
      • So we need to delete elements till dq.back() < 3   to insert 3 into dq  (2,1 are the deleted elements)
      • Resulting dq = {5, 4, 3}
  • Applications of monotonic deque :
    • It can be used to get next maximum  in a subarray (sliding-window-maximum-of-all-subarrays-of-size-k) by using monotonically decreasing deque
    • Like this it can be used to get previous maximum also in a subarray
    • It is frequently used in  sliding window problems (hard)

Other Applications:

Deques have several other applications, some of which include:

  • Palindrome checking: Deques can be used to check if a word or phrase is a palindrome. By inserting each character of the word or phrase into a deque, it is possible to check if the word or phrase is a palindrome by comparing the first and last characters, the second and second-to-last characters, and so on.
  • Graph traversal: Deques can be used to implement Breadth-First Search (BFS) on a graph. BFS uses a queue to keep track of the vertices to be visited next, and a deque can be used as an alternative to a queue in this case.
  • Task scheduler: Deques can be used to implement a task scheduler that keeps track of tasks to be executed. Tasks can be added to the back of the deque, and the scheduler can remove tasks from the front of the deque and execute them.
  • Multi-level undo/redo functionality: Deques can be used to implement undo and redo functionality in applications. Each time a user performs an action, the current state of the application is pushed onto the deque. When the user undoes an action, the front of the deque is popped, and the previous state is restored. When the user redoes an action, the next state is popped from the deque.
  • In computer science, deque can be used in many algorithms like LRU Cache, Round Robin Scheduling, Expression Evaluation.

Language Support: C++ STL provides an implementation of Deque as std::deque and Java provides the Deque interface. See this for more details. Deque in Java Deque in Python
Implementation: A Deque can be implemented either using a doubly-linked list or a circular array. In both implementations, we can implement all operations in O(1) time. We will soon be discussing the C/C++ implementation of the 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.



Similar Reads

deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::pop_front() pop_front()
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
Difference between Queue and Deque (Queue vs. Deque)
Queue: The queue is an abstract data type or linear data structure from which elements can be inserted at the rear(back) of the queue and elements can be deleted from the front(head) of the queue. The operations allowed in the queue are:insert an element at the reardelete element from the frontget the last elementget the first elementcheck the size
3 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
4 min read
Deque::empty() and deque::size() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::empty() emp
4 min read
Applications, Advantages and Disadvantages of Deque
Deque is a type of queue in which insert and deletion can be performed from either front or rear. It does not follow the FIFO rule. It is also known as double-ended queue Operations on Deque: Deque consists of mainly the following operations: Insert FrontInsert RearDelete FrontDelete Rear 1. Insert at the Front: This operation is used to add an ele
4 min read
Segregate even and odd nodes in a Linked List using Deque
Given a Linked List of integers. The task is to write a program to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. It is not needed to keep the order of even and odd nodes the same as that of the original list, the task is just to rearrange the nodes such that all even valued nodes ap
10 min read
Implement dynamic deque using templates class and a circular array
The task is to implement a dynamic Deque using templates class and a circular array, having the following functionalities: front(): Get the front item from the deque.back(): Get the last item from the deque.push_back(X): Push X at the end of the deque.push_front(X): Push X at the start of the deque.pop_front(): Delete an element from the start of t
9 min read
How to get the first and last elements of Deque in Python?
Deque is a Double Ended Queue which is implemented using the collections module in Python. Let us see how can we get the first and the last value in a Deque. Method 1: Accessing the elements by their index. The deque data structure from the collections module does not have a peek method, but similar results can be achieved by fetching the elements
2 min read
Difference between Queue and Deque in C++
Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty.size(): Returns the unsigned int, size of the que
4 min read
Implement Stack and Queue using Deque
Deque also known as double ended queue, as name suggests is a special kind of queue in which insertions and deletions can be done at the last as well as at the beginning. A link-list representation of deque is such that each node points to the next node as well as the previous node. So that insertion and deletions take constant time at both the beg
15 min read
Register Array | Introduction, Implementation and Applications
A register array is a collection of contiguous registers in computer programming and digital hardware design. Registers are small and high-speed storage units within the computer's CPU that store data temporarily for processing. A register array allows for the efficient storage and retrieval of the data elements using index-based addressing. How to
8 min read
deque::push_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::push_back() push_back()
2 min read
deque::push_front() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::push_front() push_front
2 min read
deque max_size() function in C++ STL
The deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque container can hold. Below programs illustrate the a
1 min read
deque rend() function in C++ STL
The deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the position before t
2 min read
deque rbegin() function in C++ STL
The deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the deque. Below progr
2 min read
deque insert() function in C++ STL
The deque::insert() function is a built-in function in C++ which is used to insert elements in the deque. The insert() function can be used in three ways: Extends deque by inserting a new element val at a position.Extends deque by inserting n new element of value val in the deque.Extends deque by inserting new element in the range [first, last). Sy
3 min read
deque resize() function in C++ STL
The deque::resize() is an inbuilt function in C++ STL which changes the size of the deque. If the given size is greater than the current size, then new elements are inserted at the end of the deque. If the given size is smaller than the current size, then extra elements are destroyed. Syntax: deque_name.resize(n) Parameter: The function accepts onl
2 min read
deque cbegin() in C++ STL
The cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse the queue, but not to modify it. That is, functions
2 min read
deque shrink_to_fit in C++ STL
The deque::shrink_to_fit() is a built-in function in C++ STL which reduces the capacity of the container to fit its size and destroys all elements beyond the capacity. This function does not reduce the size of the container. It is used when a container has been allocated more memory than it needed then this function makes free that amount of memory
3 min read
deque crend in C++ STL
The deque::crend() is an inbuilt function in C++ STL which returns a constant reverse iterator which points to the position before the first element of the deque. Syntax deque_name.crend() Parameters: This function does not accept any parameters. Return Type: This function returns constant reverse iterator of deque. Example-1: C/C++ Code // C++ pro
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg