WikiGalaxy

Personalize

Doubly Linked List Structure

Introduction:

A Doubly Linked List is a type of linked list in which each node contains a data part and two pointers, one pointing to the next node and another pointing to the previous node. This structure allows traversal in both forward and backward directions.

Structure Overview:

The main components of a doubly linked list are:

  • Node: Contains data and two pointers (next and prev).
  • Head: The first node in the list.
  • Tail: The last node in the list.

Diagram Representation:

      Step 1: [Head] <-> [Data | Prev | Next] <-> [Data | Prev | Next] <-> [Data | Prev | Next] <-> [Tail]
      Step 2: [Head] <-> [A | Null | B] <-> [B | A | C] <-> [C | B | Null] <-> [Tail]
    

How It Works:

Each node in a doubly linked list points to both its previous and next node. This makes it possible to traverse the list in both directions, which is an advantage over singly linked lists.

Java Implementation:


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

        public class DoublyLinkedList {
          Node head;

          public void append(int new_data) {
            Node new_node = new Node(new_data);
            Node last = head;
            new_node.next = null;

            if (head == null) {
              new_node.prev = null;
              head = new_node;
              return;
            }

            while (last.next != null)
              last = last.next;

            last.next = new_node;
            new_node.prev = last;
          }
        }
      

Advantages:

Bidirectional Traversal: Allows traversal in both directions.

Easy Deletion: Deletion of a node is easier as we have access to the previous node.

Disadvantages:

Extra Memory: Requires more memory for storing two pointers.

Complexity: Slightly more complex to implement than singly linked lists.

Console Output:

[10, 20, 30]

Detailed Explanation of Code:

The code defines a Node class with data, prev, and next pointers. The DoublyLinkedList class manages the list, with methods to append new nodes. The append method traverses to the end of the list and adds the new node, updating the next and prev pointers accordingly.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025