Deque Data Structure Last Updated : 07 Jul, 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 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(); } } BasicsArray Implementation of DequeLinked List Implementation of DequeEasy ProblemsMinimize Maximum Difference Between Adjacent 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 Rearrange Linked List to Alternate First and Last Medium ProblemsImplement 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 Deque Introduction Visit Course Deque Introduction Deque Applications Comment More infoAdvertise with us Next Article Tree Data Structure K kartik Follow Improve Article Tags : Queue DSA cpp-deque deque Practice Tags : DequeQueue Similar Reads LMNs-Data Structures Data structures are ways to organize and store data so it can be used efficiently. They are essential in computer science for managing and processing information in programs. Common types of data structures include arrays, linked lists, stacks, queues, trees, and graphs. Each structure is designed f 14 min read Tree Data Structure Tree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most 4 min read Tree Data Structure Tree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most 4 min read Trie Data Structure The Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this 15+ min read What is Data Structure? A data structure is a way of organizing and storing data in a computer so that it can be accessed and used efficiently. It refers to the logical or mathematical representation of data, as well as the implementation in a computer program.Classification:Data structures can be classified into two broad 2 min read Linked List Data Structure A linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List: 2 min read Like