Anticlockwise spiral conversion of doubly linked list in java
Java program for Anticlockwise spiral conversion of doubly linked list. Here problem description and explanation.
// Java program for
// Convert a doubly linked list anticlockwise spiral form
// This is Linked List Node
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 DoublyLinkedList
{
public LinkNode head;
public LinkNode tail;
public DoublyLinkedList()
{
// Set head and tail
this.head = null;
this.tail = null;
}
// Insert new node at end position
public void insert(int value)
{
// Create a node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
// Add first node
this.head = node;
this.tail = node;
return;
}
// Add node at last position
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
// Display node element of doubly linked list
public void display()
{
if (this.head == null)
{
System.out.println("Empty Linked List");
}
else
{
System.out.print("Linked List Head to Tail :");
// Get first node of linked list
LinkNode temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node value
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
System.out.print("\nLinked List Tail to Head :");
// Get last node of linked list
temp = this.tail;
// iterate linked list
while (temp != null)
{
// Display node value
System.out.print(" " + temp.data);
// Visit to prev node
temp = temp.prev;
}
}
}
public void spiralTransform()
{
if (this.head == null || this.head.next == null)
{
return;
}
LinkNode last = this.tail;
LinkNode current = this.head;
LinkNode auxiliary = null;
LinkNode temp = null, back = null;
// Set new head
this.head = this.tail;
while (current != null && last != null)
{
if (current == last)
{
current.next = null;
current.prev = back;
this.tail = current;
// Set new last node
return;
}
else if (current.next == last)
{
current.next = null;
current.prev = last;
last.next = current;
last.prev = back;
// Set new last node
this.tail = current;
return;
}
// Swap link
auxiliary = current.next;
temp = last.prev;
current.prev = last;
last.next = current;
current.next = temp;
last.prev = back;
back = current;
current = auxiliary;
last = temp;
}
}
public static void main(String[] args)
{
DoublyLinkedList dll = new DoublyLinkedList();
// Insert following linked list nodes
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.insert(4);
dll.insert(5);
dll.insert(6);
dll.insert(7);
dll.insert(8);
dll.insert(9);
System.out.println("\nBefore anticlockwise spiral conversion");
// Display all node
dll.display();
System.out.println("\nAfter anticlockwise spiral conversion");
dll.spiralTransform();
dll.display();
}
}
Output
Before anticlockwise spiral conversion
Linked List Head to Tail : 1 2 3 4 5 6 7 8 9
Linked List Tail to Head : 9 8 7 6 5 4 3 2 1
After anticlockwise spiral conversion
Linked List Head to Tail : 9 1 8 2 7 3 6 4 5
Linked List Tail to Head : 5 4 6 3 7 2 8 1 9
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment