Write a Java program for the following: Create a doubly linked list of elements. Delete a given element from the above list. Display the contents of the list after deletion.

public class DoublyLinkedList {


    // Node definition

    class Node {

        int data;

        Node prev;

        Node next;


        Node(int d) {

            data = d;

        }

    }


    Node head = null;

    Node tail = null;


    // Method to insert a new node at the end of the list

    public void insert(int data) {

        Node newNode = new Node(data);


        if (head == null) {

            head = newNode;

            tail = newNode;

            head.prev = null;

            tail.next = null;

        } else {

            tail.next = newNode;

            newNode.prev = tail;

            tail = newNode;

            tail.next = null;

        }

    }


    // Method to delete a given element from the list

    public void delete(int key) {

        Node current = head;


        // Case 1: List is empty

        if (head == null) {

            System.out.println("List is empty. Cannot delete.");

            return;

        }


        // Search for the node to delete

        while (current != null && current.data != key) {

            current = current.next;

        }


        // Case 2: Element not found

        if (current == null) {

            System.out.println("Element " + key + " not found in the list.");

            return;

        }


        // Case 3: Node to be deleted is the head node

        if (head == current) {

            head = current.next;

        }


        // Case 4: Node to be deleted is the tail node

        if (tail == current) {

            tail = current.prev;

        }


        // Case 5: Adjust pointers of surrounding nodes


        // If the node has a previous node, update its next pointer

        if (current.prev != null) {

            current.prev.next = current.next;

        }


        // If the node has a next node, update its previous pointer

        if (current.next != null) {

            current.next.prev = current.prev;

        }


        // Optional: clear the deleted node's pointers (garbage collection aid)

        current.prev = null;

        current.next = null;

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

    }


    // Method to display the contents of the list

    public void display() {

        Node current = head;

        if (head == null) {

            System.out.println("Doubly Linked List is empty");

            return;

        }

        System.out.print("Doubly Linked List: null <-> ");

        while (current != null) {

            System.out.print(current.data + " <-> ");

            current = current.next;

        }

        System.out.println("null");

    }


    // Main method to demonstrate the functionality

    public static void main(String[] args) {

        DoublyLinkedList dll = new DoublyLinkedList();


        // Create the list

        dll.insert(10);

        dll.insert(20);

        dll.insert(30);

        dll.insert(40);

        dll.insert(50);


        System.out.println("List before deletion:");

        dll.display();


        // Delete a middle element

        int elementToDelete1 = 30;

        dll.delete(elementToDelete1);

        System.out.println("\nList after deleting " + elementToDelete1 + ":");

        dll.display();


        // Delete the head element

        int elementToDelete2 = 10;

        dll.delete(elementToDelete2);

        System.out.println("\nList after deleting " + elementToDelete2 + ":");

        dll.display();

        

        // Delete an element not in the list

        dll.delete(99);

    }

}



Comments