Open In App

Standard Library for Deque Operations

Last Updated : 27 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);

Output
10

Time Complexity of each operation: O(1)
Auxiliary Space of each operation: O(1)



Next Article

Similar Reads