Doubly Linked List

Introduction to Doubly Linked List

In the realm of computer science and software engineering, data structures play a pivotal role in organizing and manipulating data efficiently. Among the myriad of data structures, the doubly linked list stands as a versatile and powerful tool. In this comprehensive article, we delve into the intricacies of doubly linked lists, exploring their definition, utility, characteristics, types, merits, and demerits.

Doubly Linked List

What is Doubly Linked Lists

A doubly linked list is a linear data structure comprising a sequence of elements, known as nodes, where each node contains data and two pointers/references – one pointing to the previous node (predecessor) and the other pointing to the next node (successor). This bidirectional linkage allows for efficient traversal in both forward and backward directions, distinguishing doubly linked lists from their singly linked counterparts.

Why We Use Doubly Linked Lists in Programming

Doubly linked lists are used in programming for several reasons:

  1. Bidirectional Traversal: Doubly linked lists support efficient traversal in both forward and backward directions, providing flexibility in data manipulation and access.
  2. Insertion and Deletion Operations: Doubly linked lists facilitate efficient insertion and deletion operations anywhere within the list, without the need for shifting elements, unlike arrays or singly linked lists.
  3. Memory Efficiency: Doubly linked lists offer memory efficiency by dynamically allocating memory for each node as needed, allowing for flexible memory management.
  4. Versatile Applications: Doubly linked lists find applications in various scenarios, including implementing data structures such as stacks, queues, and deques, as well as in managing data in applications such as text editors and browser history.
Doubly Linked List

Characteristics of Doubly Linked Lists

  1. Bidirectional Connectivity: Each node in a doubly linked list maintains two pointers – one pointing to the previous node and the other pointing to the next node, enabling bidirectional traversal.
  2. Dynamic Size: Doubly linked lists can dynamically grow or shrink based on the number of elements added or removed, facilitating efficient memory allocation.
  3. Node Structure: Each node in a doubly linked list contains data and two pointers/references to the previous and next nodes, forming a chain-like structure.
  4. Constant-time Insertion and Deletion: Doubly linked lists support constant-time insertion and deletion operations at any position within the list, enhancing efficiency in data manipulation.

Types of Doubly Linked List

  1. Singly Ended Doubly Linked List: In a singly ended doubly linked list, the last node points to null, allowing traversal only in the forward direction.
  2. Circular Doubly Linked Lists: A circular doubly linked list extends the concept of a singly ended doubly linked list by connecting the last node to the first node, forming a circular structure, enabling continuous traversal in both forward and backward directions.

Advantages of Doubly Linked Lists

  1. Bidirectional Traversal: Doubly linked lists support efficient bidirectional traversal, allowing for versatile data manipulation and access.
  2. Constant-time Insertion and Deletion: Doubly linked lists offer constant-time insertion and deletion operations anywhere within the lists, enhancing efficiency in data management.
  3. Memory Efficiency: Doubly linked lists utilize memory efficiently by dynamically allocating memory for each node as needed, minimizing memory wastage.
  4. Versatile Applications: Doubly linked lists find applications in various data structures and applications, ranging from implementing stacks, queues, and deques to managing data in text editors and browser history.

Dis-Advantages of Doubly Linked Lists

  1. Increased Memory Overhead: Doubly linked lists may incur increased memory overhead due to the maintenance of two pointers/references for each node, potentially increasing memory usage.
  2. Complex Implementation: Implementing and managing doubly linked lists may require additional considerations, such as handling edge cases, pointer management, and ensuring consistency in bidirectional linkage.
  3. Limited Memory Contiguity: Doubly linked lists do not guarantee memory contiguity, leading to potential memory fragmentation and reduced cache efficiency in memory-intensive applications.
  4. Potential for Node Corruption: Improper manipulation of pointers in doubly linked lists may lead to node corruption or memory leaks, necessitating careful handling and debugging.

Code Examples of Doubly Linked Lists

Below are code examples demonstrating doubly linked list usage in various programming languages:

Doubly Linked Lists in C#

using System;

public class Node {
    public int data;
    public Node prev;
    public Node next;

    public Node(int d) {
        data = d;
        prev = null;
        next = null;
    }
}

public class DoublyLinkedList {
    public Node head;

    public void PrintList() {
        Node n = head;
        while (n != null) {
            Console.Write(n.data + " ");
            n = n.next;
        }
        Console.WriteLine();
    }
}

class Program {
    static void Main(string[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        dll.head.next = second;
        second.prev = dll.head;
        second.next = third;
        third.prev = second;
        dll.PrintList();
    }
}

Output

1 2 3

Doubly Linked Lists in C

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
};

void PrintList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 1;
    head->prev = NULL;
    head->next = second;

    second->data = 2;
    second->prev = head;
    second->next = third;

    third->data = 3;
    third->prev = second;
    third->next = NULL;

    PrintList(head);

    return 0;
}

Output

1 2 3

Doubly Linked Lists in C++

#include <iostream>

class Node {
public:
    int data;
    Node* prev;
    Node* next;

    Node(int d) : data(d), prev(nullptr), next(nullptr) {}
};

class DoublyLinkedList {
public:
    Node* head;

    void PrintList() {
        Node* current = head;
        while (current != nullptr) {
            std::cout << current->data << " ";
            current = current->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    DoublyLinkedList dll;
    dll.head = new Node(1);
    Node* second = new Node(2);
    Node* third = new Node(3);
    dll.head->next = second;
    second->prev = dll.head;
    second->next = third;
    third->prev = second;
    dll.PrintList();

    return 0;
}

Output

1 2 3

Doubly Linked Lists in Python

class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def printList(self):
        current = self.head
        while current:
            print(current.data, end=" ")
            current = current.next
        print()

dll = DoublyLinkedList()
dll.head = Node(1)
second = Node(2)
third = Node(3)
dll.head.next = second
second.prev = dll.head
second.next = third
third.prev = second
dll.printList()

Output

1 2 3

Doubly Linked Lists in PHP

<?php
class Node {
    public $data;
    public $prev;
    public $next;

    public function __construct($d) {
        $this->data = $d;
        $this->prev = null;
        $this->next = null;
    }
}

class DoublyLinkedList {
    public $head;

    public function printList() {
        $current = $this->head;
        while ($current != null) {
            echo $current->data . " ";
            $current = $current->next;
        }
        echo "\n";
    }
}

$dll = new DoublyLinkedList();
$dll->head = new Node(1);
$second = new Node(2);
$third = new Node(3);
$dll->head->next = $second;
$second->prev = $dll->head;
$second->next = $third;
$third->prev = $second;
$dll->printList();
?>

Output

1 2 3

Doubly Linked Lists in JAVA

class Node {
    int data;
    Node prev, next;

    public Node(int d) {
        data = d;
        prev = next = null;
    }
}

class DoublyLinkedList {
    Node head;

    void printList(Node node) {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
        System.out.println();
    }
}

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        dll.head.next = second;
        second.prev = dll.head;
        second.next = third;
        third.prev = second;
        dll.printList(dll.head);
    }
}

Output

1 2 3

Doubly Linked Lists in JavaScript

class Node {
    constructor(data) {
        this.data = data;
        this.prev = null;
        this.next = null;
    }
}

class DoublyLinkedList {
    constructor() {
        this.head = null;
    }

    printList() {
        let current = this.head;
        while (current !== null) {
            console.log(current.data);
            current = current.next;
        }
    }
}

let dll = new DoublyLinkedList();
dll.head = new Node(1);
let second = new Node(2);
let third = new Node(3);
dll.head.next = second;
second.prev = dll.head;
second.next = third;
third.prev = second;
dll.printList();

Output

1
2
3

Conclusion

In conclusion, doubly linked lists represent a powerful and versatile data structure, offering efficient bidirectional traversal, constant-time insertion and deletion operations, and dynamic memory management. Despite their complexities and potential overhead, doubly linked lists find widespread applications in various domains, including implementing data structures such as stacks, queues, and deques, as well as managing data in applications such as text editors and browser history. By mastering the concepts and applications of doubly linked lists, programmers can enhance their problem-solving skills and develop robust, efficient algorithms, contributing to the advancement of technology and innovation in the digital age.

For more visit my website Codelikechamp.com

What is Data Structure

Types of data structure

what is Linked List

what is array

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top