Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Queue
  • Practice Queue
  • MCQs on Queue
  • Queue Tutorial
  • Operations
  • Applications
  • Implementation
  • Stack vs Queue
  • Types of Queue
  • Circular Queue
  • Deque
  • Priority Queue
  • Stack using Queue
  • Advantages & Disadvantages
Open In App
Next Article:
Deque - Introduction and Applications
Next article icon

Deque - Introduction and Applications

Last Updated : 11 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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 Queue
  • It 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 Languages

  • deque in C++
  • Deque in Java
  • deque in Python
  • Deque in JavaScript

Below 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 Basics

  • Array Implementation of Deque
  • Linked List Implementation of Deque

Practice Problems Based on Deque

Basic Problems

  • Difference between Queue and Deque
  • Deque Implementation in Python
  • First and Last Elements of Deque in Python
  • Add Element at Front of a Deque
  • Remove an Element from Front of Deque
  • Minimize Maximum Difference Between Adjacent Elements

Easy Problems

  • Rearrange Linked List to Alternate First and Last
  • Substring with Maximum Frequency
  • Prefixes as Suffixes of a String
  • Level order traversal in spiral form
  • String after processing backspace characters
  • Generate a Sequence by inserting positions
  • Lexicographically largest permutation
  • Check if Strings Can Be Made Equal

Medium Problems

  • Stack and Queue Using ArrayDeque in Java
  • Implement Stack and Queue using Deque
  • Generate Bitonic Sequence
  • Rearrange Array Elements
  • Longest Subarray with Absolute Difference ≤ X
  • Reverse a Linked List in groups
  • Max Sum Subsequence with K Distant Elements
  • Nth term of given recurrence relation
  • Max Subarray Length with K Increments
  • Largest String after Deleting K Characters
  • Segregate 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

Next Article
Deque - Introduction and Applications

K

kartik
Improve
Article Tags :
  • Queue
  • DSA
  • cpp-deque
  • deque
Practice Tags :
  • Deque
  • Queue

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences