reverse a doubly circular linked list by swapping node value in scala
Scala program for reverse a doubly circular linked list by swapping node value. Here more information.
/*
Scala Program
Reverse a doubly circular linked list by swapping value
*/
// Linked list node
class LinkNode(var data: Int,
var next: LinkNode,
var back: LinkNode);
class CircularDoublyLL(var front: LinkNode,
var tail: LinkNode)
{
def this()
{
this(null, null);
}
// Add new node at the end of linked list
def addNode(data: Int): Unit = {
var node: LinkNode = new LinkNode(data, null, null);
// When new node is successfully created
if (this.front == null)
{
// Add first node of linked list
this.front = node;
this.tail = node;
}
else
{
// Add node at last position
this.tail.next = node;
node.back = this.tail;
this.tail = node;
}
// Connect the last node in the linked list to front
this.tail.next = this.front;
this.front.back = this.tail;
// Set node data
node.data = data;
}
// Print linked list nodes
def printElement(): Unit = {
if (this.front == null)
{
print("\n Empty Linked List \n");
}
else
{
var temp: LinkNode = this.front;
print("\n Linked List from Front to Tail \n");
// Display doubly linked list from start to last node
while (temp != null)
{
print(" " + temp.data);
if (temp == this.tail)
{
// When get last node
temp = null;
}
else
{
temp = temp.next;
}
}
temp = this.tail;
print("\n Linked List from Tail to Front \n");
// Display doubly linked list from last to start node
while (temp != null)
{
print(" " + temp.data);
if (temp == this.front)
{
// When get first node
temp = null;
}
else
{
temp = temp.back;
}
}
}
}
// Reverses the elements of doubly linked list
// using swap node values
def reverseElement(): Unit = {
// Get first node
var first: LinkNode = this.front;
// Get last node
var last: LinkNode = this.tail;
var temp: Int = 0;
// Reverse operation
while (first != null && last != null)
{
if (first != last)
{
// Swap the node value
temp = first.data;
first.data = last.data;
last.data = temp;
if (first.next == last)
{
// Stop reverse operation, or use break
first = null;
last = null;
}
else
{
first = first.next;
last = last.back;
}
}
else
{
// Stop reverse operation, or use break
first = null;
last = null;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var cll: CircularDoublyLL = new CircularDoublyLL();
// Add linked list node
cll.addNode(1);
cll.addNode(2);
cll.addNode(3);
cll.addNode(4);
cll.addNode(5);
cll.addNode(6);
print("\n Before reverse linked list");
cll.printElement();
cll.reverseElement();
print("\n\n After reverse linked list");
cll.printElement();
}
}
Output
Before reverse linked list
Linked List from Front to Tail
1 2 3 4 5 6
Linked List from Tail to Front
6 5 4 3 2 1
After reverse linked list
Linked List from Front to Tail
6 5 4 3 2 1
Linked List from Tail to Front
1 2 3 4 5 6
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