Double Linked List in Java.

A Double-Linked List in Java is a data structure and a kind of linked list. It’s also known as a DLL, and each node of a double-linked list contains three parts.

  • Data – The value it stores.
  • Prev – Reference to the previous node.
  • Next – Reference to the next node.

Double-Linked List Structure in Java.

In Java, we can define a node as shown in the given example.

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

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

Java Program with Functionalities.

Here is an example of a Java program that demonstrates a Doubly Linked List with its functionalities.

  • Insert nodes
  • Traverse (Show Data, Bidirectional)
  • Delete a node
  • Add a new node at a specific position

Java Program of a Double-Linked List

// Node definition for Doubly Linked List

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

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


class DoubleLinkedList {

    Node head = null;  // First node

   
 // Insert at the end
    public void insert(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node temp = head;
            while (temp.next != null)
                temp = temp.next;
            temp.next = newNode;
            newNode.prev = temp;
        }
    }


    // Traverse forward
    public void traverseForward() {
        Node temp = head;
        System.out.print("Forward: ");
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    // Traverse backward
    public void traverseBackward() {
        Node temp = head;
        if (temp == null) 
		return;

        // Go to last node
        while (temp.next != null)
            temp = temp.next;

        System.out.print("Backward: ");
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.prev;
        }
        System.out.println();
    }


    // Delete a node with given data
    public void delete(int key) {
        Node temp = head;

        while (temp != null && temp.data != key)
            temp = temp.next;

        if (temp == null) {
            System.out.println("Node not found!");
            return;
        }

        if (temp.prev != null)
            temp.prev.next = temp.next;
        else
            head = temp.next; // deleting head

        if (temp.next != null)
            temp.next.prev = temp.prev;

        System.out.println("Deleted: " + key);
    }


    // Add node at specific position
    public void insertAtPosition(int data, int position) {
        Node newNode = new Node(data);

        if (position <= 0) {
            System.out.println("Invalid position!");
            return;
        }

        if (position == 1) {
            newNode.next = head;
            if (head != null)
                head.prev = newNode;
            head = newNode;
            return;
        }

        Node temp = head;
        for (int i = 1; temp != null && i < position - 1; i++)
            temp = temp.next;

        if (temp == null) {
            System.out.println("Position out of bounds!");
            return;
        }

        newNode.next = temp.next;
        if (temp.next != null)
            temp.next.prev = newNode;
        temp.next = newNode;
        newNode.prev = temp;
    }

    
    // Main method
    public static void main(String[] args) {
        DoubleLinkedList dll = new DoubleLinkedList();

        dll.insert(10);
        dll.insert(20);
        dll.insert(30);
        dll.insert(40);

        dll.traverseForward();   // Output: 10 20 30 40
        dll.traverseBackward();  // Output: 40 30 20 10

        dll.insertAtPosition(15, 2);
        dll.traverseForward();   // Output: 10 15 20 30 40

        dll.delete(30);
        dll.traverseForward();   // Output: 10 15 20 40
    }
}

Explanation of the function in the given example

  • insert() – Adds a new node at the end of the list
  • traverseForward() – Prints the list from head to tail
  • traverseBackward() – Prints the list from tail to head
  • delete(int key) – Searches and removes the node containing key
  • insertAtPosition(data, position) – Inserts a node at a specific position