What Is Doubly Ended Queue in Data Structure?

//

Larry Thompson

A Doubly Ended Queue, also known as a Deque, is a fundamental data structure that allows efficient insertion and deletion of elements from both ends. It combines the features of both stacks and queues, making it a versatile data structure for various applications.

Overview:
A Doubly Ended Queue supports insertion and deletion operations at both the front and rear ends. This means that elements can be added or removed from either end based on the requirement. It provides flexibility in managing data by allowing access to elements from both directions.

Key Features:

  • Double-ended operations: A Deque supports insertions and deletions at both ends, making it easy to add or remove elements as needed.
  • Random access: Unlike some other linear data structures, a Doubly Ended Queue allows direct access to any element by its index position.
  • Dynamic size: The size of a Deque can dynamically grow or shrink based on the number of elements added or removed.

Implementation of a Doubly Ended Queue:

To implement a Doubly Ended Queue in programming, we can use an array or linked list. Here, we will discuss the implementation using a linked list.

Data Structure for Node:
To create a linked list-based implementation of a Deque in most programming languages, we need to define a node structure with two pointers: one for the previous node and another for the next node.

C++ Implementation:

#include <iostream>
using namespace std;

// Structure representing a node
struct Node {
  int data;
  Node* prev;
  Node* next;
};

// Class representing a Doubly Ended Queue
class Deque {
  Node* front;
  Node* rear;

  // Deque operations:
  

  • // Insert an element at the front end of the Deque
  • void insertFront(int value) {
  •    

    • // Create a new node
    • Node* newNode = new Node();
    • // Set the value of the node to the given value
    • newNode->data = value;
    •    

      • // If the Deque is empty, set both front and rear pointers to the new node and return
      •    

        •   If (front == NULL) {
        •  front = rear = newNode;

           return;

        // Otherwise, set the previous pointer of the current front node to the new node and update front pointer to point to the new node.

         front->prev = newNode;
         newNode->next = front;
         front = newNode;

  • // Insert an element at the rear end of the Deque
  • void insertRear(int value) {
  •    

    • // Create a new node
    • Node* newNode = new Node();
    • // Set the value of the node to the given value
    • newNode->data = value;
    •    

      •  // If the Deque is empty, set both front and rear pointers to the new node and return
      • If (rear == NULL) {
          front = rear = newNode;
          return;

       // Otherwise, set the next pointer of the current rear node to the new node and update rear pointer to point to the new node.

      rear->next = newNode;

      newNode->prev = rear;

      rear = newNode;
       

  • // Delete an element from the front end of the Deque
  • void deleteFront() {
  •   

    • // If the Deque is empty, return as there is nothing to delete
    • If (front == NULL)
    • return;

       // Store the front node in a temporary variable
      Node* temp = front;

       // Update the front pointer to point to the next node
      front = front->next;

       // If after deletion, the Deque becomes empty, update rear pointer as well
      If (front == NULL)

        rear = NULL;

  • // Delete an element from the rear end of the Deque
  • // Similar to deleteFront(), but update rear pointer this time
  • Conclusion:

    A Doubly Ended Queue is a powerful data structure that allows efficient insertion and deletion operations at both ends. It combines the functionality of both stacks and queues, providing flexibility in managing data. By using appropriate HTML styling elements like bold text, underlined text, lists, and subheaders, we can create visually engaging and organized content for readers.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy