Wstaw węzeł na końcu podwójnie połączonej listy
// Java Program
// Insert node at end of circular doubly linked list
// Define class of linked list Node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode prev;
public LinkNode(int data)
{
// Set node value
this.data = data;
this.next = null;
this.prev = null;
}
}
public class CircularDLL
{
public LinkNode head;
public LinkNode tail;
public CircularDLL()
{
// Set head and tail values
this.head = null;
this.tail = null;
}
// Insert node at end of circular doubly linked list
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
// First node of linked list
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;
// Set new last 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("\nNode 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("\nNode 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();
// Add following linked list nodes
cdll.insert(1);
cdll.insert(2);
cdll.insert(3);
cdll.insert(4);
cdll.insert(5);
cdll.insert(6);
// Display node
cdll.headToTail();
cdll.tailToHead();
}
}
Cruel Chicken