# JAVA Program for Insertion in the End 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.

## 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 end of the Doubly Linked List, we’ll have to store and redirect various links of the Linked List.
• First of all the next pointer of the previously last Node will store the address of the New Node that is being Inserted in the Linked List.
• Now Since the New Node is going to be the last Node of the Linked List. So, the Next Pointer of the New Node will point to Tail.
• Then the Previous Pointer of the New Node will now point to the Previously Last Node of the Linked List.

## Algorithm to be used for the Insertion of a Node at the End of a Doubly Linked List

• appendAtEnd (data)
• HEAD = TAIL = newNode
• TAIL.next = NULL
• Else
• TAIL.next = newNode
• newNode.prev = TAIL
• TAIL = newNode
• TAIL.next = NULL

## Java Program for Insertion in the end of a Doubly Linked List

```    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() {
while (temp != null) {
System.out.print(temp.data + "–>");
// Set temp to point to the next node
temp = temp.next;
}
System.out.println("END");
}
//appendAtEnd function will add a node to the end of the list
public void appendAtEnd(int data) {
//Create a new node
Node newNode = new Node(data);
//Check if the list is empty
//Both head and tail will point towards the newNode
//head's previous will point towards 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;
}
}
//print() will print the nodes of the doubly linked list
void print() {
//Node current will point to head
System.out.println("List is empty");
return;
}
System.out.println("Appending a node to the end of the list: ");
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) {
PrepIdList = new PrepInsta();
//Appending 10 to the list
dList.appendAtEnd(10);
dList.print();
//Appending 20 to the list
dList.appendAtEnd(20);
dList.print();
//Appending 30 to the list
dList.appendAtEnd(30);
dList.print();              //Appending 40 to the list              dList.appendAtEnd(40);            dList.print();
//Appending 50 to the list
dList.appendAtEnd(50);
dList.print();
}
}```