Doubly Linked List in java

Previous
Next

In this post, we will see about Doubly LinkedList implementation in java.
We have already seen the implementation of singly linked list. You can consider this as an extension of Singly linked list.It is quite complex to implement it as compared to singly linked list.
In doubly linked list, Node has data and pointers to next node and previous node. First node’s previous points to null and Last node‘s next also points to null, so you can iterate over linked list in both direction with these next and previous pointers.
An example of Doubly Linked List:
Doubly Linked List in java
Node for doubly linked list can be presented as below:

As you can see, we have one more extra reference(Node prev) in case of doubly linked list.
Let’s say, you want to do insert First operation in case of Doubly linked list, it will look like as below:

Insert Doubly Linked List in java

Doubly linked list in java example

Let’s implement Doubly Linked List in java.

Create a java file named DoublyLinkedList.java.

Create another class named DoubleLinkedListMain as below:

When you run above program, you will get below output:

Printing Doubly LinkedList (head –> tail)
{ 1 }
{ 7 }
{ 6 }
{ 5 }
{ 2 }Printing Doubly LinkedList (tail –> head)
{ 2 }
{ 5 }
{ 6 }
{ 7 }
{ 1 }================
Printing Doubly LinkedList (head –> tail)
{ 1 }
{ 6 }
{ 5 }
{ 2 }

Printing Doubly LinkedList (tail –> head)
{ 2 }
{ 5 }
{ 6 }
{ 1 }

================
Printing Doubly LinkedList (head –> tail)
{ 6 }
{ 5 }

Printing Doubly LinkedList (tail –> head)
{ 5 }
{ 6 }

That’s all about Doubly linked list in java.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog