Open In App

Deque - Introduction and Applications

Last Updated : 01 Jul, 2025
Suggest changes
Share
Like Article
Like
Report

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
  • 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

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

Practice Problems Based on Deque

Basic Problems

Easy Problems

Medium Problems


Next Article

Similar Reads

Article Tags :
Practice Tags :