VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implementation-deque-using-doubly-linked-list/

⇱ Implementation of Deque using doubly linked list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Deque using doubly linked list

Last Updated : 19 Sep, 2025

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.

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

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

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)

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)

Complete Implementation:


Output
Rear: 10
New Rear: 5
Front: 15
Size: 2
New Front: 5
Comment
Article Tags: