In Previous Post, How to insert a node at the beginning of a Doubly Linked List in Java ? we learned how to insert a Node at the beginning of a Doubly LinkedList.
In this post, we will learn How to insert a node at the end of a Doubly Linked List in Java?
Logic is very simple:
If the head is null that means LinkedList is empty so make newNode as head Node else newNode assign to tail’s next and assign tail to newNode’s previous, finally assign newNode to tail.
Below is the complete Source code:
LinkedList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package com.kkjavatutorials.util; /** * @author KK JavaTutorials * Custom Doubly LinkedList Implementation * In Java */ public class LinkedList<T> { //Pointer to first node private Node<T> head; //Pointer to last node. private Node<T> tail; /** * @author KK JavaTutorials * This internal class represent * Node of Doubly Linked List */ private static class Node<E> { private E data; private Node<E> next; private Node<E> previous; public Node(Node<E> previous, E data, Node<E> next) { this.data = data; this.next = next; this.previous = previous; } } /** * Method to insert at the End of * a Doubly Linked List * @param e Element to be inserted */ public void addLast(T e) { final Node<T> newNode = new Node<>(null, e, null); /** * if head is null that means LinkedList is empty so * make newNode as head Node * else newNode assign to tail's next and assign * tail to newNode's previous * finally assign newNode to tail */ if (head == null) head = newNode; else { tail.next = newNode; newNode.previous = tail; } tail = newNode; } /** * Linked List Printing in forward direction */ public void displayLinkedListInForward() { if(head == null) { return; }else { while(head != null) { System.out.print(head.data+" "); head = head.next; } } } /** * Linked List Printing in Backward direction */ public void displayLinkedListInBackward() { if(tail == null) { return; }else { while(tail != null) { System.out.print(tail.data+" "); tail = tail.previous; } } } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.kkjavatutorials.client; import com.kkjavatutorials.util.LinkedList; /** * Client program to show demo * Demo to insert node at the End of * a Doubly Linked List in Java ? * @author KK JavaTutorials * */ public class ClientTest { public static void main(String[] args) { //Create an Instance of LinkedList LinkedList<Integer> linkedList = new LinkedList<>(); //Call addLast few times linkedList.addLast(10); linkedList.addLast(20); linkedList.addLast(30); System.out.println("-----Print Linked List in Forward Direction-------"); linkedList.displayLinkedListInForward(); System.out.println(); System.out.println("-----Print Linked List in Backward Direction-------"); linkedList.displayLinkedListInBackward(); } } |
The output of this Program:
—–Print Linked List in Forward Direction——-
10 20 30
—–Print Linked List in Backward Direction——-
30 20 10
You May Also Like:
How to represent the Doubly Linked List in Java?
How to insert a node at the beginning of a Doubly Linked List in Java ?
Representation of Singly Linked List in Java ?
How to insert a node at the beginning of a Singly Linked List in Java?
How to Insert node at the end of a Singly Linked List in Java ?
How to insert a node in Linked List at a given position in Java ?
How to remove the first node from a Singly Linked List in Java ?
How to remove the last node from a Singly Linked List in Java
How to remove a node from a Singly Linked List at a given position in Java?
How to remove a given key from the Singly Linked List in Java ?
How to find the middle node in a Singly Linked List in Java ?
How to search an element in a Singly Linked List in Java ?
That’s all about the How to insert a node at the end of a Doubly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.