JAVA program for Insertion in the End of a Doubly Linked List

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.

insertion at end in doubly linked list

Steps to be followed while Inserting a Node at the End 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.

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.

Insertion at end in a doubly linked list

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

  • appendAtEnd (data)
  • if(HEAD == NULL)
    • HEAD = TAIL = newNode
    • HEAD.prev = NULL
    • 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

Run
import java.lang.*;

class DoublyLinkedList
{
  Node head;
// not using parameterized constructor would by default
// force head instance to become null
// Node head = null; // can also do this, but not required

// Node Class
  class Node
  {
    int data;
    Node next, prev;

      Node (int x)		// parameterized constructor
    {
      data = x;
      next = null;
      prev = null;
    }
  }



  public void insertEnd (int data)
  {
    // Creating newNode memory & assigning data value
    Node freshNode = new Node (data);

    // assign data
    // since this will be the last node its next will be NULL
    freshNode.next = null;

    //if we are entering the first node
    if (head == null)
      {
	head = freshNode;
	freshNode.prev = null;
	return;
      }

    Node last = head;

    // traverse to the current last node
    while (last.next != null)
      last = last.next;

    // assign current last node's next to this new node
    // assign new node's previous to this last node
    last.next = freshNode;
    freshNode.prev = last;
    // new_node becomes the last node now

  }



  public void printList ()
  {
    Node node = head;
    Node end = null;
//as linked list will end when Node reaches Null

    System.out.print ("\nIn forward: ");
    while (node != null)
      {
	System.out.print (node.data + " ");
	end = node;
	node = node.next;
      }
    System.out.print ("\nIn backward: ");

    while (end != null)
      {
	System.out.print (end.data + " ");
	end = end.prev;
      }
    System.out.println ();
  }

}

class Main
{

  public static void main (String args[])
  {
    DoublyLinkedList doublylist = new DoublyLinkedList ();

      doublylist.insertEnd (3);
      doublylist.insertEnd (2);
      doublylist.insertEnd (1);
      doublylist.insertEnd (4);
      doublylist.insertEnd (5);

      doublylist.printList ();

  }
}

Output

In forward: 3 2 1 5 4 
In backward: 4 5 1 2 3 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java

Doubly Linked List