Organize linked list into groups of even and odd nodes in typescript
Ts program for Organize linked list into groups of even and odd nodes. Here problem description and explanation.
/*
TypeScript program for
Grouping of even and odd nodes in linked list
*/
// Linked list node
class LinkNode
{
public data: number;
public next: LinkNode;
constructor(data: number)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
public head: LinkNode;
constructor()
{
this.head = null;
}
// Adding new node at beginning of linked list
public insert(data: number)
{
// Create new node
var node = new LinkNode(data);
// Connect current node to head
node.next = this.head;
this.head = node;
}
// Display linked list element
public display()
{
if (this.head == null)
{
return;
}
var temp = this.head;
var result = "";
// iterating linked list elements
while (temp != null)
{
result += " " + temp.data + " →";
// Visit to next node
temp = temp.next;
}
console.log(result," NULL");
}
// Arrange alternative even and odd nodes in linked list
public arrangeEvenOdds()
{
// Define some auxiliary variables
var even_node: LinkNode = null;
var odd_node: LinkNode = null;
var tail_1: LinkNode = null;
var tail_2: LinkNode = null;
var auxiliary = this.head;
this.head = null;
// Segregation of even and odd nodes elements
while (auxiliary != null)
{
if (auxiliary.data % 2 == 0)
{
// Even Node element
if (even_node == null)
{
even_node = auxiliary;
}
else
{
// Add node at end of even list
tail_1.next = auxiliary;
}
// Get new last node
tail_1 = auxiliary;
// visit to next node
auxiliary = auxiliary.next;
// set next node is null
tail_1.next = null;
}
else
{
// Odd Node element
if (odd_node == null)
{
odd_node = auxiliary;
}
else
{
// Add node at end of odd list
tail_2.next = auxiliary;
}
// Get new last node
tail_2 = auxiliary;
// visit to next node
auxiliary = auxiliary.next;
// set next node is null
tail_2.next = null;
}
}
if (even_node != null)
{
// new first node of linked list
this.head = even_node;
}
else
{
// When only odd element exists in linked list
this.head = odd_node;
}
// Combine even and odd nodes
while (even_node != null && odd_node != null)
{
// next node of even list
tail_1 = even_node.next;
// next node of odd list
tail_2 = odd_node.next;
even_node.next = odd_node;
if (tail_1 != null)
{
// This is useful to handle
// when even linked list next node is empty
// and odd elements are next node not empty.
odd_node.next = tail_1;
}
// Visit to next node
even_node = tail_1;
odd_node = tail_2;
}
}
public static main(args: string[])
{
var list1 = new SingleLL();
var list2 = new SingleLL();
var list3 = new SingleLL();
// Create linked list1
list1.insert(8);
list1.insert(2);
list1.insert(9);
list1.insert(7);
list1.insert(3);
list1.insert(10);
list1.insert(5);
list1.insert(4);
list1.insert(6);
// Create linked list2
list2.insert(1);
list2.insert(1);
list2.insert(4);
list2.insert(2);
list2.insert(3);
list2.insert(1);
// Create linked list3
list3.insert(2);
list3.insert(3);
list3.insert(3);
list3.insert(8);
list3.insert(4);
list3.insert(6);
list3.insert(2);
// Test A
console.log(" Before arrange : ");
// Before arrange nodes
list1.display();
// Case A
// When linked list contains same length of
// Even and Odd nodes
list1.arrangeEvenOdds();
console.log(" After arrange : ");
// After arrange nodes
list1.display();
// Test B
console.log("\n Before arrange : ");
// Before arrange nodes
list2.display();
// Case B
// When linked list Odd nodes are more than Even
list2.arrangeEvenOdds();
console.log(" After arrange : ");
// After arrange nodes
list2.display();
// Test C
console.log("\n Before arrange : ");
// Before arrange nodes
list3.display();
// Case C
// When linked list Odd nodes are more than Even
list3.arrangeEvenOdds();
console.log(" After arrange : ");
// After arrange nodes
list3.display();
}
}
SingleLL.main([]);
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/
Output
Before arrange :
6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → NULL
After arrange :
6 → 5 → 4 → 3 → 10 → 7 → 2 → 9 → 8 → NULL
Before arrange :
1 → 3 → 2 → 4 → 1 → 1 → NULL
After arrange :
2 → 1 → 4 → 3 → 1 → 1 → NULL
Before arrange :
2 → 6 → 4 → 8 → 3 → 3 → 2 → NULL
After arrange :
2 → 3 → 6 → 3 → 4 → 8 → 2 → NULL
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