Java - Doubly Linked List
I have given here Java sample code for doubly linked list, filling elements and traversal in forward and backward direction. I have NOT given the code for deleting the nodes. It is easy to implement by changing the insert functions.
Singly Linked List, Doubly Linked List, Singly Linked Circular List and Doubly Linked Circular List are an important data structure used in Java applications.
Related Links:
- Turbo C++ - Reverse Singly Linked List
- Turbo C++ - Singly Linked Circular List
- Turbo C++ - Doubly Linked List
- Turbo C++ - Singly Linked List
- Visual C++ Program to Reverse Singly Linked List
- Visual C++ Program for Singly Linked Circular List
- Visual C++ Program for Doubly Linked List
- Visual C++ Program for Singly Linked List
- Java - Reverse Singly Linked List
- Java - Singly Linked Circular List
- Java - Doubly Linked List
- Java - Singly Linked List
- C# - Reverse Singly Linked List
- C# - Singly Linked Circular List
- C# - Doubly Linked List
- C# - Singly Linked List
Source Code
import java.lang.*;
import java.util.*;
import java.io.*;
class DLinkedList
{
private int data;
private DLinkedList next;
private DLinkedList prev;
public DLinkedList()
{
data = 0;
next = null;
prev = null;
}
public DLinkedList(int value)
{
data = value;
next = null;
prev = null;
}
public DLinkedList InsertNext(int value)
{
DLinkedList node = new DLinkedList(value);
if(this.next == null)
{
// Easy to handle
node.prev = this;
node.next = null; // already set in constructor
this.next = node;
}
else
{
// Insert in the middle
DLinkedList temp = this.next;
node.prev = this;
node.next = temp;
this.next = node;
temp.prev = node;
// temp.next does not have to be changed
}
return node;
}
public DLinkedList InsertPrev(int value)
{
DLinkedList node = new DLinkedList(value);
if(this.prev == null)
{
node.prev = null; // already set on constructor
node.next = this;
this.prev = node;
}
else
{
// Insert in the middle
DLinkedList temp = this.prev;
node.prev = temp;
node.next = this;
this.prev = node;
temp.next = node;
// temp.prev does not have to be changed
}
return node;
}
public void TraverseFront()
{
TraverseFront(this);
}
public void TraverseFront(DLinkedList node)
{
if(node == null)
node = this;
System.out.println("\n\nTraversing in Forward Direction\n\n");
while(node != null)
{
System.out.println(node.data);
node = node.next;
}
}
public void TraverseBack()
{
TraverseBack(this);
}
public void TraverseBack(DLinkedList node)
{
if(node == null)
node = this;
System.out.println("\n\nTraversing in Backward Direction\n\n");
while(node != null)
{
System.out.println(node.data);
node = node.prev;
}
}
public static void main(String[] args)
{
DLinkedList node1 = new DLinkedList(1);
DLinkedList node3 = node1.InsertNext(3);
DLinkedList node2 = node3.InsertPrev(2);
DLinkedList node5 = node3.InsertNext(5);
DLinkedList node4 = node5.InsertPrev(4);
node1.TraverseFront();
node5.TraverseBack();
}
}
Output
Traversing in Forward Direction
1
2
3
4
5
Traversing in Backward Direction
5
4
3
2
1
|