In this source code example, we will write a Java program to demonstrate how to implement Doubly Linked List in Java.
A doubly linked list is a linear data structure that consists of a sequence of nodes, where each node stores a reference to an element and references to the previous and next nodes in the sequence. The nodes are linked together in both directions, so it is called a doubly linked list.
Doubly Linked List Overview
Doubly Linked List Implementation in Java
import java.util.NoSuchElementException;
public class DoublyLinkedList {
private ListNode head;
private ListNode tail;
private int length;
private class ListNode {
private int data; // Can be any generic type
private ListNode next;
private ListNode previous;
public ListNode(int data) {
this.data = data;
}
}
public DoublyLinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
public boolean isEmpty() {
return length == 0; // or head == null
}
public int length() {
return length;
}
public void displayForward() {
if (head == null) {
return;
}
ListNode temp = head;
while (temp != null) {
System.out.print(temp.data + " --> ");
temp = temp.next;
}
System.out.println("null");
}
public void displayBackward() {
if (head == null) {
return;
}
ListNode temp = tail;
while (temp != null) {
System.out.print(temp.data + " --> ");
temp = temp.previous;
}
System.out.println("null");
}
public void insertFirst(int value) {
ListNode newNode = new ListNode(value);
if (isEmpty()) {
tail = newNode;
} else {
head.previous = newNode;
}
newNode.next = head;
head = newNode;
length++;
}
public void insertEnd(int value) {
ListNode newNode = new ListNode(value);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
newNode.previous = tail;
}
tail = newNode;
length++;
}
public ListNode deleteFirst() {
if (isEmpty()) {
throw new NoSuchElementException();
}
ListNode temp = head;
if (head == tail) {
tail = null;
} else {
head.next.previous = null;
}
head = head.next;
temp.next = null;
length--;
return temp;
}
public ListNode deleteLast() {
if (isEmpty()) {
throw new NoSuchElementException();
}
ListNode temp = tail;
if (head == tail) {
head = null;
} else {
tail.previous.next = null;
}
tail = tail.previous;
temp.previous = null;
length--;
return temp;
}
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertEnd(1);
dll.insertEnd(2);
dll.insertEnd(3);
dll.displayForward();
dll.deleteLast();
dll.deleteLast();
dll.displayForward();
}
}
Output:
1 --> 2 --> 3 --> null
1 --> null
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course