You are on page 1of 3

11/7/22, 9:34 PM Deque | Set 1 (Introduction and Applications) - GeeksforGeeks

DSA Array Matrix Write & Earn Strings Hashing Linked List Stack Queue Binary Tree

Deque | Set 1 (Introduction and Applications)


Difficulty Level :
Easy ● Last Updated :
29 Apr, 2022

Deque or Double Ended Queue is a generalized version of Queue data structure that

allows inser t and delete at both ends.

Operations on Deque : Mainly the following four basic operations are per formed on

queue:

inser tFront(): Adds an item at the front of Deque.

inser tLast(): Adds an item at the rear of Deque.

deleteFront(): Deletes an item from the front of Deque.

deleteLast(): Deletes an item from the rear of Deque. In addition to the above

operations, the following operations are also suppor ted.

getFront(): Gets the front item from the 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 suppor ts both stack and queue operations, it can be

used as both. The Deque data structure suppor ts clock wise and anticlock wise rotations

in O(1) time which can be useful in cer tain 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:

https://www.geeksforgeeks.org/deque-set-1-introduction-applications/ 1/5
11/7/22, 9:34 PM Deque | Set 1 (Introduction and Applications) - GeeksforGeeks

Start Your Coding Journey Now! Login Register

Applied as both stack and queue, as it suppor ts both operations.

Storing a web browser ’s histor y.

Storing a sof tware application’s list of undo operations.

Job scheduling algorithm

Language Suppor t : C++ STL provides an implementation of Deque as std::deque and

Java provides the Deque inter face. See this for more details. Deque in Java Deque in

P ython

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.

Recommended

Solve DSA problems on GfG Practice. Solve Problems

https://www.geeksforgeeks.org/deque-set-1-introduction-applications/ 2/5
11/7/22, 9:34 PM Deque | Set 1 (Introduction and Applications) - GeeksforGeeks

Start Your Coding Journey Now! Like 167


Login Register

Previous Next

RECOMMENDED ARTICLES Page : 1 2 3

deque::begin() and deque::end in deque::at() and deque::swap() in


01 05
C++ STL C++ STL
16, Jan 18 11, Jan 18

Deque::front() and deque::back() in deque::clear() and deque::erase()


02 06
C++ STL in C++ STL
15, Dec 17 26, Dec 17

deque::pop_front() and deque::operator= and


03 07
deque::pop_back() in C++ STL deque::operator[] in C++ STL
15, Dec 17 09, Jan 18

Deque::empty() and deque::size() in deque::emplace_front() and


04 08
C++ STL deque::emplace_back() in C++ STL
15, Dec 17 18, Jan 18

Ar ticle Contributed By :

GeeksforGeeks

https://www.geeksforgeeks.org/deque-set-1-introduction-applications/ 3/5

You might also like