reverse a doubly circular linked list by swapping node value in typescript
Ts program for reverse a doubly circular linked list by swapping node value. Here problem description and other solutions.
/*
TypeScript Program
Reverse a doubly circular linked list by swapping value
*/
// Linked list node
class LinkNode
{
public data: number;
public next: LinkNode;
public back: LinkNode;
constructor(data: number, back: LinkNode, next: LinkNode)
{
this.data = data;
this.back = back;
this.next = next;
}
}
class CircularDoublyLL
{
public front: LinkNode;
public tail: LinkNode;
constructor()
{
this.front = null;
this.tail = null;
}
// Add new node at the end of linked list
public addNode(data: number)
{
var node = 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
public printElement()
{
if (this.front == null)
{
console.log("\n Empty Linked List \n");
}
else
{
var temp = this.front;
console.log("\n Linked List from Front to Tail \n");
// Display doubly linked list from start to last node
while (temp != null)
{
console.log(" " + temp.data);
if (temp == this.tail)
{
// When get last node
temp = null;
}
else
{
temp = temp.next;
}
}
temp = this.tail;
console.log("\n Linked List from Tail to Front \n");
// Display doubly linked list from last to start node
while (temp != null)
{
console.log(" " + 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
public reverseElement()
{
// Get first node
var first = this.front;
// Get last node
var last = this.tail;
var temp = 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;
}
}
}
public static main(args: string[])
{
var cll = new CircularDoublyLL();
// Add linked list node
cll.addNode(1);
cll.addNode(2);
cll.addNode(3);
cll.addNode(4);
cll.addNode(5);
cll.addNode(6);
console.log("\n Before reverse linked list");
cll.printElement();
cll.reverseElement();
console.log("\n\n After reverse linked list");
cll.printElement();
}
}
CircularDoublyLL.main([]);
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/
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