Open In App

Implementation of Deque using doubly linked list

Last Updated : 19 Sep, 2025
Comments
Improve
Suggest changes
1 Like
Like
Report

Doubly Linked List implementation of deque allows constant-time insertion and deletion at both ends. We only need to maintain pointers or references to both the ends, front and rear.

deque-using-ll

Operations on Deque

The following four basic operations are typically performed on a deque:

  • insertFront(): Adds an item at the front.
  • insertRear(): Adds an item at the rear.
  • deleteFront(): Removes the front item.
  • deleteRear(): Removes the rear item.

Additionally, the following operations are also supported:

  • getFront(): Retrieves the front item
  • getRear(): Retrieves the last item
  • isEmpty(): Checks if the deque is empty.
  • size(): Returns the number of elements in the deque.
  • erase(): Removes all elements

All operations on a deque,take O(1) time and O(1) extra space. Only the erase() operation takes O(n) time, since it removes all elements from the deque.

Doubly Linked List Representation of Deque:

  1. Define a Doubly Linked List Node structure containing, data, prev and next
  2. Declare two pointers, front and rear, of type Node and initialize both as NULL to represent an empty deque.
C++
class Node {
public:

    int data;
    Node* prev;
    Node* next;
    Node(int data) { 
        this->data = data; 
        prev = next = nullptr; 
    }
};

class myDeque {
    Node* front;
    Node* rear;
public:

    myDeque() { 
        front = rear = nullptr; 
    }
}
Java Python C# JavaScript

Insertion at Front :

  1. Allocate a new node (newNode) for the doubly linked list.
  2. Check if the deque is empty:
    => If front == NULL, set both front and rear to newNode.
  3. If the deque is not empty:
    => Set newNode->next = front (link new node to current front).
    => Set front->prev = newNode (link current front back to new node).
    => Update front = newNode (move front pointer to new node).
C++
void insertFront(int data) {
    Node* newNode = new Node(data);
        
    // empty deque
    if (!front) { 
        front = rear = newNode;
    } else {
        newNode->next = front;
        front->prev = newNode;
        front = newNode;
    }
}
Java Python C# JavaScript

Insertion at Rear :

  1. Allocate a new node (newNode) for the doubly linked list.
  2. Check if the deque is empty:
    => If rear == NULL, set both front and rear to newNode.
  3. If the deque is not empty:
    => Set newNode->prev = rear (link new node back to current rear).
    => Set rear->next = newNode (link current rear forward to new node).
    => Update rear = newNode (move rear pointer to new node).
C++
void insertRear(int data) {
    Node *newNode = new Node(data);

    // empty deque
    if (rear == nullptr) {
        front = rear = newNode;
    }
    else {
        newNode->prev = rear;
        rear->next = newNode;
        rear = newNode;
    }
}
Java Python C# JavaScript

Deletion from Front end :

  1. Check if the deque is empty:
    => If front == NULL, print "Underflow" and exit.
  2. Store the current front node:
    => temp = front
  3. Move the front pointer to the next node:
    => front = front->next
  4. Update rear or front pointers:
    => If front == NULL, the deque is now empty, so set rear = NULL
    => Else, set front->prev = NULL to detach the old front node
  5. Deallocate memory of the removed node:
    => delete temp (or equivalent in your language)
C++
void deleteFront() {
    
    // empty deque
    if (front == nullptr) { 
        cout << "Underflow\n";
        return;
    }
    
    Node* temp = front;
    front = front->next;
    
    if (front) front->prev = nullptr;
    else rear = nullptr;
    delete temp;
    
    size--;
}
Java Python C# JavaScript

Deletion from Rear end :

  1. Check if the deque is empty:
    => front == NULL (or rear == NULL), print "Underflow" and exit.
  2. Store the current rear node:
    => temp = rear
  3. Move the rear pointer to the previous node:
    => rear = rear->prev
  4. Update front or rear pointers:
    => If rear == NULL, the deque is now empty, so set front = NULL
    => Else, set rear->next = NULL to detach the old rear node
  5. Deallocate memory of the removed node:
    => delete temp (or equivalent in your language)
C++
void deleteRear() {
    if (rear == nullptr) {
        cout << "Underflow\n";
        return;
    }
    Node* temp = rear;
    rear = rear->prev;

    if (rear == nullptr) 
        front = nullptr;
    else
        rear->next = nullptr;

    delete temp;
    size--;
}
Java Python C# JavaScript

Complete Implementation:

C++
#include <iostream>
using namespace std;

// Node class for doubly linked list
class Node {
public:
    int data;
    Node *prev, *next;

    Node(int data) { 
        this->data = data; 
        prev = nullptr; 
        next = nullptr; 
    }
};

// Deque implementation using doubly linked list
class myDeque {
    Node *front, *rear;
    int size;

public:
    myDeque() { 
        front = nullptr; 
        rear = nullptr; 
        size = 0; 
    }

    bool isEmpty() { return front == nullptr; }
    int getSize() { return size; }

    // Insert at front
    void insertFront(int data) {
        Node* newNode = new Node(data);
       
        if (isEmpty()) front = rear = newNode;
        else {
            newNode->next = front;
            front->prev = newNode;
            front = newNode;
        }
       
        size++;
    }

    // Insert at rear
    void insertRear(int data) {
        Node* newNode = new Node(data);
        
        if (isEmpty()) front = rear = newNode;
        else {
            newNode->prev = rear;
            rear->next = newNode;
            rear = newNode;
        }
        size++;
    }

    // Delete from front
    void deleteFront() {
        if (isEmpty()) cout << "UnderFlow\n";
        else {
            front = front->next;
            if (front) front->prev = nullptr;
            else rear = nullptr;
            size--;
        }
    }

    // Delete from rear
    void deleteRear() {
        if (isEmpty()) cout << "UnderFlow\n";
        else {
            rear = rear->prev;
            if (rear) rear->next = nullptr;
            else front = nullptr;
         
            size--;
        }
    }

    // Get front element
    int getFront() { return isEmpty() ? -1 : front->data; }

    // Get rear element
    int getRear() { return isEmpty() ? -1 : rear->data; }

    // Clear the deque
    void erase() {
        while (!isEmpty()) deleteFront();
    }
};

int main() {
    myDeque dq;

    dq.insertRear(5);
    dq.insertRear(10);
    cout << "Rear: " << dq.getRear() << endl;

    dq.deleteRear();
    cout << "New Rear: " << dq.getRear() << endl;

    dq.insertFront(15);
    cout << "Front: " << dq.getFront() << endl;
    cout << "Size: " << dq.getSize() << endl;

    dq.deleteFront();
    cout << "New Front: " << dq.getFront() << endl;

    return 0;
}
C Java Python C# JavaScript

Output
Rear: 10
New Rear: 5
Front: 15
Size: 2
New Front: 5

Article Tags :

Explore