JAVA Program for Deletion from Beginning in a Doubly Linked List

JAVA Program to Delete a Node from the Beginning of a Doubly Linked Last

Over here in this program we will create a linked list and add a node at the end of doubly linked list. To insert a new node at the end of a linked list we have to check that the list is empty or not. If the list is empty both head and tail points towards the newly added node. If it is not empty, in such case add a new node at the end and make sure that tail points towards the newly added node.

Example: If we have a list (10 –> 20 –> 30 –> 40) and we have to add a new node which is 50. So after adding new node. Updated Linked list will be (10 –> 20 –> 30 –>40 –> 50) . We will be learning more about the whole process in the explanation below.

JAVA Program to Delete a Node from the Beginning of a Doubly Linked List

Steps to be followed while Inserting a Node at the Beginning of a Doubly Linked List

  • Check for the presence of Node in the List, if there exists some Nodes, Continue.
  • Now, to Delete a node from the end of the Doubly Linked List, we’ll have to create and redirect multiple links in the Linked List.
  • Foremost, the link between the First Node and the head and the link between the Previous Pointer of the First Node and the null will also be broken.
  • Now Since the Second Node is going to be the First Node of the Linked List. So, the Head will now Point to the address of the Second Node.
  • Then the Previous Pointer of the Second Node will now point to Null.
JAVA Program for Deletion from Beginning in a Doubly Linked List

Algorithm to be used for Deletion from the Beginning of a Doubly Linked List

  • IF(HEAD == NULL)
    • RETURN 
  • ELSE IF(HEAD != TAIL)
    • TAIL = TAIL.PREV
    • TAIL.NEXT = NULL 
  • ELSE 
    • HEAD = TAIL = NULL 

 

Java Program for Deletion from the Beginning of a Doubly Linked List

   public class PrepInsta
    {  
        //Constitiute a node of the doubly linked list  
        class Node{  
            int data;  
            Node prev;  
            Node next;  
            public Node(int data) {  
                this.data = data;  
            }  
        }  
    // Function to traverse and print the linked list
    public void display() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + "–>");
            // Set temp to point to the next node
            temp = temp.next;
        }
        System.out.println("END");
    }
    //Constitiute the head and tail of the doubly linked list  
    Node head, tail = null; 
    public void addNode(int data) {  
        //Create a new node  
        Node newNode = new Node(data);  
        //Check if the list is empty  
        if(head == null) {  
            //Both head and tail will point towards the newNode  
            head = tail = newNode;  
            //head's previous will point towards null  
            head.prev = null;  
            //tail's next will point towards null, as it is the last node of the list  
            tail.next = null;  
        }  
        //Append newNode as new tail of the list  
        else {  
            //newNode will be added after tail such that tail's next will point to newNode  
            tail.next = newNode;  
            //newNode's previous will point to tail  
            newNode.prev = tail;  
            //newNode will become new tail  
            tail = newNode;  
            //As it is last node, tail's next will point to null  
            tail.next = null;  
        }  
    }
    public void deleteInitial() {  
        if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }  
        else {  
            //testing for the presence of a single node in the list, If not, Then head and tail will be re-directed
            if(head != tail) {  
                head = head.next;  
            }  
            //if only one node exist both head and tail will be redirected to null
            else {  
                head = tail = null;  
            }  
        }  
    }  
    //print() will print the nodes of the doubly linked list 
      void print() {  
        //Node current will point to head  
        Node curr = head;  
        if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }
        while(curr != null) 
           {  
            //Prints each node by increasing order of the pointer   
            System.out.print(curr.data + " ");  
            curr = curr.next;  
            }  
        System.out.println();  
        }  
    public static void main(String[] args) {  
        PrepInsta dList = new PrepInsta(); 
        dList.addNode(10);  
        dList.addNode(20);  
        dList.addNode(30);
        dList.addNode(40);
        dList.addNode(50);
        System.out.println("Initial Doubly Linked List: "); 
        dList.print();
        dList.deleteInitial();
        System.out.println("Doubly Linked List after Deletion from Beginning: "); 
        dList.print();  
    }  
}
Initial Doubly Linked List: 
10 20 30 40 50 
Doubly Linked List after Deletion from Beginning: 
20 30 40 50