JAVA Program for Insertion at the Beginning of a Doubly Linked List

Java Program for insertion at the beginning in doubly linked list

Adding or inserting a node in the beginning in doubly linked list is almost similar as the process of adding a node in singly linked list . The only difference is that we have an extra pointer (previous node) to be redirected.We will create a doubly linked list and insert every new node at the beginning of the list.

Example of Insertion in the Beginning of a Doubly Linked List-

If we have doubly linked listed like, (1–>2–>3–>4) and we have to add 5 at the beginning of the list then after adding linked list would look like  (5–>1–>2–>3–>4 ) .

JAVA Program to Insert a Node at 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 insert a node in the beginning of the Doubly Linked List, we’ll have to store and redirect various links of the Linked List.
  • First of all the Head will now store the address of the space where the data of the New Node is stored.
  • Now Since the New Node is going to be the first Node of the Linked List. So, the Previous Pointer of the New Node will point to Null.
  • Then the Next Pointer of the New Node will now point to the Previously First Node of the List.
  • Now, at last the Previous Pointer of the Previously First node of the list will be directed towards the New Node that is being Inserted.
JAVA Program for Insertion at Beginning in a Doubly Linked List

Algorithm to write a function to add a Node in the Beginning of a Linked List

  • AppendStart(int data)
  • Node newNode = new Node(data)
  • IF HEAD == NULL
    • newNode HEAD = TAIL = newNode
    • HEAD.previous = NULL
    • TAIL.next = NULL
  • ELSE
    • HEAD.previous = newNode
    • newNode.next = HEAD
    • newNode.previous = NULL
    •  HEAD = newNode

JAVA Program to Insert a Node at the Beginning of a Linked List

     import java.util.*;
     
     class PrepInsta 
     {  
          
        //Constitute a node of the doubly linked list  
      
        class Node{  
            int data;  
            Node previous;  
            Node next;  
              
            public Node(int data) {  
                this.data = data;  
            }  
        }  
          
        //constitute the head and tail of the doubly linked list  
        Node head, tail = null;  
        // 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");
    }
          
        //Function to add a node to the starting of the list  
        public void AppendStart(int data) {  
            //Create a new Node  
            Node newNode = new Node(data);  
              
            //If list is empty  
            if(head == null) {  
                //head and tail both will point to newNode  
                head = tail = newNode;  
                //head's previous will point to null  
                head.previous = null;  
                //tail's next will point to null, because it is the last node of the list  
                tail.next = null;  
            }  
            //make newNode as new head of the list  
            else {  
                //head's previous pointer node will be newNode  
                head.previous = newNode;  
                //newNode's next pointer node will be head  
                newNode.next = head;  
                //newNode's pointer previous will point to null  
                newNode.previous = null;  
                //newNode will become new head  
                head = newNode;  
            }  
        }  
          
        //print() will print the nodes of the list  
        public void print() {  
            //current Node will point to head  
            Node curr = head;  
            if(head == null) {  
                System.out.println("List is empty");  
                return;  
            }  
            System.out.println("Append a node to the start of the list: ");  
            while(curr != null) {  
                //Prints each node by incrementing the pointer.  
      
                System.out.print(curr.data + " ");  
                curr = curr.next;  
            }  
            System.out.println();  
        }  
          
        public static void main(String[] args) {  
              
            PrepInsta dList = new PrepInsta();  
              
            //Appending 5 to the list  
            dList.AppendStart(5);  
            dList.print();  
            //Appending 4 to the list  
            dList.AppendStart(4);  
            dList.print();  
            //Appending 3 to the list  
            dList.AppendStart(3);  
            dList.print();  
            //Appending 2 to the list  
            dList.AppendStart(2);  
            dList.print();  
            //Appending 1 to the list  
            dList.AppendStart(1);  
            dList.print();  
        }  
    }

Output:

Append a node to the start of the list:
5
Append a node to the start of the list:
4 5
Append a node to the start of the list:
3 4 5
Append a node to the start of the list:
2 3 4 5
Doubly linked list after printing:
1 2 3 4 5