In this post, We will learn How to insert a node at the beginning of a Doubly Linked List in Java?
Logic is Very Simple:
Initially, We assign head to the first pointer and if first is pointer is null that means LinkedList is empty so make newNode as tail Node so that head and tail both pointing to the same Node else newNode assign to first’s previous pointer in case LinkedList is not empty
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 beginning of * a Doubly Linked List * @param e Element to be inserted */ public void addFirst(T e) { final Node<T> first = head; final Node<T> newNode = new Node<>(null, e, first); head = newNode; /** * As first is currently pointing to head and if * first is null that means LinkedList is empty so * make newNode as tail Node so that head and tail * both pointing to same Node * else newNode assign to first.previous * in case LinkedList is not empty */ if (first == null) tail = newNode; else first.previous = 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 * This program shows How to insert node at the beginning 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 addFirst few times linkedList.addFirst(10); linkedList.addFirst(20); linkedList.addFirst(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——-
30 20 10
—–Print Linked List in Backward Direction——-
10 20 30
You May Also Like:
How to represent the 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 beginning of a Doubly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.