Deque - Introduction and Applications Last Updated : 11 May, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends. Below is an example program of deque in different languages.Deque can act as both Stack and QueueIt is useful in many problems where we need to have a subset of all operations also like insert/remove at front and insert/remove at the end.It is typically implemented either using a doubly linked list or circular array.Implementations of Deque in Different Languagesdeque in C++Deque in Javadeque in PythonDeque in JavaScriptBelow are example programs in different languages. C++ #include <iostream> #include <deque> using namespace std; int main() { deque<int> dq; dq.push_back(10); dq.push_back(20); dq.push_front(30); // Print deque elements for (int x : dq) cout << x << " "; cout << endl; // Pop from front and back dq.pop_front(); dq.pop_back(); // Print deque elements after pop for (int x : dq) cout << x << " "; return 0; } Java import java.util.ArrayDeque; import java.util.Deque; public class Main { public static void main(String[] args) { Deque<Integer> dq = new ArrayDeque<>(); dq.addLast(10); dq.addLast(20); dq.addFirst(30); // Print deque elements for (int x : dq) System.out.print(x + " "); System.out.println(); // Pop from front and back dq.removeFirst(); dq.removeLast(); // Print deque elements after pop for (int x : dq) System.out.print(x + " "); } } Python from collections import deque dq = deque() dq.append(10) dq.append(20) dq.appendleft(30) # Print deque elements print(' '.join(map(str, dq))) # Pop from front and back dq.popleft() dq.pop() # Print deque elements after pop print(' '.join(map(str, dq))) C# using System; using System.Collections.Generic; class Program { static void Main() { Deque<int> dq = new Deque<int>(); dq.AddLast(10); dq.AddLast(20); dq.AddFirst(30); // Print deque elements foreach (int x in dq) Console.Write(x + " "); Console.WriteLine(); // Pop from front and back dq.RemoveFirst(); dq.RemoveLast(); // Print deque elements after pop foreach (int x in dq) Console.Write(x + " "); } } public class Deque<T> { private LinkedList<T> list = new LinkedList<T>(); public void AddFirst(T value) { list.AddFirst(value); } public void AddLast(T value) { list.AddLast(value); } public void RemoveFirst() { list.RemoveFirst(); } public void RemoveLast() { list.RemoveLast(); } public IEnumerator<T> GetEnumerator() { return list.GetEnumerator(); } } Deque BasicsArray Implementation of DequeLinked List Implementation of DequePractice Problems Based on DequeBasic ProblemsDifference between Queue and DequeDeque Implementation in PythonFirst and Last Elements of Deque in PythonAdd Element at Front of a DequeRemove an Element from Front of DequeMinimize Maximum Difference Between Adjacent ElementsEasy ProblemsRearrange Linked List to Alternate First and Last Substring with Maximum FrequencyPrefixes as Suffixes of a String Level order traversal in spiral formString after processing backspace charactersGenerate a Sequence by inserting positionsLexicographically largest permutation Check if Strings Can Be Made Equal Medium ProblemsStack and Queue Using ArrayDeque in JavaImplement Stack and Queue using DequeGenerate Bitonic Sequence Rearrange Array Elements Longest Subarray with Absolute Difference ≤ X Reverse a Linked List in groups Max Sum Subsequence with K Distant ElementsNth term of given recurrence relation Max Subarray Length with K Increments Largest String after Deleting K CharactersSegregate even and odd nodes in a Linked List Generate Permutation with Unique Adjacent Differences 0-1 BFS Min Deques to Sort Array Min Number by Applying + and * Operations Comment More infoAdvertise with us Next Article Deque - Introduction and Applications K kartik Follow Improve Article Tags : Queue DSA cpp-deque deque Practice Tags : DequeQueue Similar Reads 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 4 min read Introduction and Array Implementation of Deque A Deque (Double-Ended Queue) is a data structure that allows elements to be added or removed from both endsâfront and rear. Unlike a regular queue, which only allows insertions at the rear and deletions from the front, a deque provides flexibility by enabling both operations at either end. This make 3 min read Introduction to Divide and Conquer Algorithm Divide and Conquer Algorithm is a problem-solving technique used to solve problems by dividing the main problem into subproblems, solving them individually and then merging them to find solution to the original problem. Divide and Conquer is mainly useful when we divide a problem into independent su 9 min read Real-life Applications of Data Structures and Algorithms (DSA) You may have heard that DSA is primarily used in the field of computer science. Although DSA is most commonly used in the computing field, its application is not restricted to it. The concept of DSA can also be found in everyday life. Here we'll address the common concept of DSA that we use in our d 10 min read Applications, Advantages and Disadvantages of Depth First Search (DFS) Depth First Search is a widely used algorithm for traversing a graph. Here we have discussed some applications, advantages, and disadvantages of the algorithm. Applications of Depth First Search:1. Detecting cycle in a graph: A graph has a cycle if and only if we see a back edge during DFS. So we ca 4 min read Like