class LinkNode
{
public int data;
public LinkNode next;
public LinkNode prev;
public LinkNode(int data)
{
this.data = data;
this.next = null;
this.prev = null;
}
}
public class CircularDLL
{
public LinkNode head;
public LinkNode tail;
public CircularDLL()
{
this.head = null;
this.tail = null;
}
public void insert(int value)
{
LinkNode node = new LinkNode(value);
if (this.head == null)
{
this.head = node;
this.tail = node;
node.next = node;
node.prev = node;
}
else
{
node.next = this.head;
node.prev = this.tail;
this.head.prev = node;
this.tail.next = node;
this.tail = node;
}
}
public void headToTail()
{
if (this.head == null)
{
System.out.println("Empty linked list");
}
else
{
LinkNode temp = this.head;
System.out.println("
Node Form Front to Rear :");
while (temp != null)
{
System.out.print(temp.data + " ");
temp = temp.next;
if (temp == this.head)
{
return;
}
}
}
}
public void tailToHead()
{
if (this.tail == null)
{
System.out.print("Empty linked list");
}
else
{
LinkNode temp = this.tail;
System.out.println("
Node Form Rear to Front :");
while (temp != null)
{
System.out.print(temp.data + " ");
temp = temp.prev;
if (temp == this.tail)
{
return;
}
}
}
}
public static void main(String[] args)
{
CircularDLL cdll = new CircularDLL();
cdll.insert(1);
cdll.insert(2);
cdll.insert(3);
cdll.insert(4);
cdll.insert(5);
cdll.insert(6);
cdll.headToTail();
cdll.tailToHead();
}
}