Doubly Linked List Program in Java

Doubly linked list programs are very complex programs to understand because the node of the doubly linked list contains two fields, previous and next. In C and C++, it is very easy to maintain a doubly linked list using pointers, but in Java, there is no concept of pointer that makes its construction a little bit tricky. A doubly linked list program can be of creating a doubly-linked list, inserting a node, or deleting a node.

Let's understand some basic programs of doubly linked list:

Creating a doubly linked list

For creating a node, we have to create a class for creating a Node type. The class contains three properties, i.e., data, prev, and next. The data can be of int, String, or float and prev and next are of the Node type. The user stores the information, and prev and next contain the previous and next nodes of the doubly linked list. In the given program, each line of code is defined through comments so that you can understand the code easily.

CreateDoublyLinkedList.java

Output:

Doubly Linked List Program in Java

Inserting a Node into a doubly-linked list

In the doubly linked list, a node can be inserted into one of the following ways:

  1. At the beginning of the doubly linked list.
  2. Add a node at the specified position.
  3. Add a node at the end of the list.

We will understand all these ways by creating their programs one by one.

Insert a node in the beginning

In the CreateDoublyLinkedList example, the newly created node is added in the last of the doubly linked list. When we need to add a node at the beginning of the list, we need to change the value of the head node and the node which was pointed by that head. So, we will use the same example which we create above. We only add a method to that program for adding a node at the beginning of the list.

AddNodeInBeginning.java

Output:

Doubly Linked List Program in Java

Insert a node at the end

We can add a node at the end of the list too. In the CreateDoublyLinkedList program, each newly created node ad at the end of the list. So, we can say that the code of inserting a node at the end is already discussed before. But to make it more specific, we have given another example in which we create a separate method for adding a node.

AddNodeAtEnd.java

Output:

Doubly Linked List Program in Java

Insert a node at the specified position

In order to create a program for adding a node at the specified position, we have to focus on the following four cases:

  1. When the doubly linked list is empty, or the position is not available in the list.
  2. The specified position can be 1, so we have to create the code for adding the node at the beginning of the list, which we discussed above.
  3. The position can also be the last index or position in the doubly linked list. So, we also have to write the code for adding a node in the last of the list.
  4. The last case is when the position is available somewhere in between the head and tail.

In the given code, we create separate methods for each case and try to make it as simple as possible.

AddNodeAtSpecifiedLocation.java

Output:

Doubly Linked List Program in Java

Deleting a Node from a doubly linked list

For deleting a node, three cases are possible:

  1. When the node is the first node in the list.
  2. When the node is the last node in the list.
  3. When the node is available in between the first and the last node.

In the given program, we have created three methods for these three different cases. Each line of code is described through comments in the program to make it easy to understand.

DeleteNodeFromList.java

Output:

Doubly Linked List Program in Java
Next TopicJava 12




Latest Courses