Standard Library for Deque Operations
Last Updated :
27 Mar, 2025
A deque (double-ended queue) is a data structure that allows elements to be added or removed from both ends, providing greater flexibility than a standard queue. It combines the features of both stacks and queues, making it useful for various applications where efficient manipulation of elements from both ends is required. Deque operations such as insertion, deletion, and traversal offer powerful solutions to many algorithmic problems.
Given a deque and q queries. The task is to perform some operation on dequeue according to the queries as given below:
1. pb: query to push back the element x.
2. pf: query to push element x(given with query) to the front of the deque.
3. pp_b(): query to delete element from the back of the deque.
4. f: query to return a front element from the deque.
Examples:
Input: queries = [[pf 5], [pf 10], [pb 6], [f], [pp_b]]
Output: 10
Explanation:
1. After push front deque will be [5]
2. After push front deque will be [10, 5]
3. After push back deque will be [10, 5, 6]
4. Return front element which is 10
5. After pop back deque will be [10, 5]
Input: queries = [[pf 5], [f]]
Output: 5
Explanation:
1. After push front deque will be [5]
2. Return front element which is 5
1. push_back Operation: The push_back operation is used to add an element to the back of the deque. This operation increases the size of the deque by one, and the element is inserted at the rear end. It’s a common operation when you need to add new data while keeping the front end unchanged.
2. pop_back Operation: The pop_back operation removes the element from the back of the deque. It decreases the size of the deque by one and shifts the remaining elements accordingly. This is useful when you need to process or remove the most recently added elements.
3. front Operation: The front operation is used to access the element at the front of the deque without removing it. It returns the first element in the deque, allowing you to inspect or process it while keeping the rest of the data intact.
4. push_front Operation: The push_front operation inserts an element at the front of the deque. This operation shifts all other elements towards the back, making space for the new element at the beginning. It is helpful when the new data should be added at the start while maintaining the order of existing elements.
C++
#include <deque>
#include <iostream>
using namespace std;
void pb(deque<int>& dq, int x) { dq.push_back(x); }
void ppb(deque<int>& dq) {
if (!dq.empty()) dq.pop_back();
}
int front_dq(deque<int>& dq) {
if (!dq.empty()) return dq.front();
return -1;
}
void pf(deque<int>& dq, int x) { dq.push_front(x); }
int main() {
deque<int> dq;
pf(dq, 5);
pf(dq, 10);
pb(dq, 6);
cout << front_dq(dq) << endl;
ppb(dq);
return 0;
}
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GfG {
static void pb(Deque<Integer> dq, int x) { dq.addLast(x); }
static void ppb(Deque<Integer> dq) {
if (!dq.isEmpty()) dq.removeLast();
}
static int front_dq(Deque<Integer> dq) {
if (!dq.isEmpty()) return dq.getFirst();
return -1;
}
static void pf(Deque<Integer> dq, int x) { dq.addFirst(x); }
public static void main(String[] args) {
Deque<Integer> dq = new ArrayDeque<>();
pf(dq, 5);
pf(dq, 10);
pb(dq, 6);
System.out.println(front_dq(dq));
ppb(dq);
}
}
Python
from collections import deque
def pb(dq, x):
dq.append(x)
def ppb(dq):
if dq:
dq.pop()
def front_dq(dq):
if dq:
return dq[0]
return -1
def pf(dq, x):
dq.appendleft(x)
if __name__ == '__main__':
dq = deque()
pf(dq, 5)
pf(dq, 10)
pb(dq, 6)
print(front_dq(dq))
ppb(dq)
C#
using System;
using System.Collections.Generic;
class GfG {
static void pb(LinkedList<int> dq, int x) { dq.AddLast(x); }
static void ppb(LinkedList<int> dq) {
if (dq.Count > 0) dq.RemoveLast();
}
static int front_dq(LinkedList<int> dq) {
if (dq.Count > 0) return dq.First.Value;
return -1;
}
static void pf(LinkedList<int> dq, int x) { dq.AddFirst(x); }
static void Main() {
var dq = new LinkedList<int>();
pf(dq, 5);
pf(dq, 10);
pb(dq, 6);
Console.WriteLine(front_dq(dq));
ppb(dq);
}
}
JavaScript
let dq = [];
function pb(dq, x) { dq.push(x); }
function ppb(dq) {
if (dq.length > 0) dq.pop();
}
function front_dq(dq) {
if (dq.length > 0) return dq[0];
return -1;
}
function pf(dq, x) { dq.unshift(x); }
pf(dq, 5);
pf(dq, 10);
pb(dq, 6);
console.log(front_dq(dq));
ppb(dq);
Time Complexity of each operation: O(1)
Auxiliary Space of each operation: O(1)
Similar Reads
Operations on Deque
A Deque (Double-Ended Queue) is a linear data structure that allows elements to be added or removed from both endsâfront and rear. Unlike a regular queue that adds and removes elements from one end (FIFO) or a stack that works by adding and removing elements from the top (LIFO), a deque allows you t
4 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
Stack Implementation using Deque
A doubly ended queue or deque allows insertion and deletion at both ends. In a stack, we need to do insertions and deletions at one end only. We can use either end of deque (front or back) to implement a stack, DequeIn the below implementation, we use back (or rear) of stack to do both insertions an
2 min read
Deque vs List in Python
Python provides multiple data structures to handle collections of data, with list and deque (double-ended queue) being two commonly used structures, each offering unique advantages depending on the operation at hand.List is a dynamic array that supports indexing and slicing.Deque is a doubly linked
4 min read
Implementation of Deque using doubly linked list
A Deque (Double-Ended Queue) is a data structure that allows adding and removing elements from both the front and rear ends. Using a doubly linked list to implement a deque makes these operations very efficient, as each node in the list has pointers to both the previous and next nodes. This means we
9 min read
Deque Interface in Java
Deque Interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports adding or removing elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/
9 min read
Deque meaning in DSA
Deque, which stands for Double Ended Queue, is a special type of queue that allows adding and removing elements from both front and rear ends. Deque Data StructureCharacteristics: of Deque:Dynamic size: The size of a deque can change dynamically during the execution of a program.Linear: Elements in
2 min read
Deque in Python
A deque stands for Double-Ended Queue. It is a data structure that allows adding and removing elements from both ends efficiently. Unlike regular queues, which are typically operated on using FIFO (First In, First Out) principles, a deque supports both FIFO and LIFO (Last In, First Out) operations.E
6 min read
Implementation of Deque using Array - Simple
A Deque (Double-Ended Queue) is a data structure that allows insertion and deletion of elements at both ends (front and rear). This flexibility makes it more versatile than a regular queue, where insertion and deletion can only happen at one end. In this article, we will explore how to implement a d
7 min read
Deque of Pairs in C++ with Examples
What is a deque? In C++, the deque is a sequence container and it is also known by the name, double-ended queue. As the name implies, a deque allows insertion and deletion from both ends. Although a deque is similar to a vector, deques are more efficient compared to vectors. In vectors, contiguous s
5 min read