JAVA Program for Deletion from End of a Doubly Linked List

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

Each variation of Data Structure has some basic list of operations that can be performed on them one of them is Deletion from Various Nodes. Here in this page we’ll take a look at the detailed explanation of the Delete Operation from the End of a Doubly Linked List.

Example: If we have a list (10 –> 20 –> 30 –> 40 –> 50) and we have to Delete the node storing 50. So after Deleting the Last Node. Updated Linked list will be (10 –> 20 –> 30 –>40) . We will be learning more about the whole process in the explanation below.

JAVA Program to Delete a Node from the end 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 redirect links of the Linked List.
  • Let the length of the List = i.
  • First of all the next pointer of the (i-1)th node of the Linked List will now be re-directed towards the Tail of the Linked List.
JAVA Program for Deletion from the end in a Doubly Linked List

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

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

 

Java Program for Insertion in the end 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;  
    //appendAtEnd function will add a node to the end of the list  
    public void addNode(int data) {  
        //Create a new node  
        Node newNode = new Node(data);  
        //Check if the list is empty  
        if(head == null) {  
            head = tail = newNode;  
            head.prev = null;  
            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 deleteLast() {  
        if(head == null) {  
            return;  
        }  
        else {  
            if(head != tail) {  
                tail = tail.prev;  
                tail.next = 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.deleteLast();
        System.out.println("Doubly Linked List after Deletion from End: "); 
        dList.print();  
    }  
}
Initial Doubly Linked List: 
10 20 30 40 50 
Doubly Linked List after Deletion from End: 
10 20 30 40