Skip to main content

Insertion sort on doubly linked list in node js

Js program for Insertion sort on doubly linked list. Here more information.

// Node JS program for
// Perform insertion sort on doubly linked list
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
		this.prev = null;
	}
}
class DoublyLinkedList
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Insert new node at end position
	insert(value)
	{
		// Create a node
		var 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
	display()
	{
		if (this.head == null)
		{
			console.log("Empty Linked List");
		}
		else
		{
			process.stdout.write("\nLinked List Head to Tail :");
			// Get first node of linked list
			var temp = this.head;
			// iterate linked list 
			while (temp != null)
			{
				// Display node value
				process.stdout.write("  " + temp.data);
				// Visit to next node
				temp = temp.next;
			}
			process.stdout.write("\nLinked List Tail to Head :");
			// Get last node of linked list
			temp = this.tail;
			// iterate linked list 
			while (temp != null)
			{
				// Display node value
				process.stdout.write("  " + temp.data);
				// Visit to prev node
				temp = temp.prev;
			}
			process.stdout.write("\n");
		}
	}
	// Swap value of given node
	swapData(first, second)
	{
		var value = first.data;
		first.data = second.data;
		second.data = value;
	}
	// Sort elements using insertion sort
	insertionSort()
	{
		// Get first node
		var front = this.head;
		var back = null;
		while (front != null)
		{
			// Get next node
			back = front.next;
			// Update node value when consecutive nodes are not sort
			while (back != null && 
                   back.prev != null && 
                   back.data < back.prev.data)
			{
				// Modified node data
				this.swapData(back, back.prev);
				// Visit to previous node
				back = back.prev;
			}
			// Visit to next node
			front = front.next;
		}
	}
}

function main()
{
	var dll = new DoublyLinkedList();
	//Insert element of linked list
	dll.insert(25);
	dll.insert(2);
	dll.insert(6);
	dll.insert(14);
	dll.insert(12);
	dll.insert(9);
	dll.insert(16);
	dll.insert(3);
	process.stdout.write("\n Before Sort");
	dll.display();
	// Display all node
	dll.insertionSort();
	process.stdout.write("\n After Sort");
	dll.display();
}
// Start program execution
main();

Output

 Before Sort
Linked List Head to Tail :  25  2  6  14  12  9  16  3
Linked List Tail to Head :  3  16  9  12  14  6  2  25

 After Sort
Linked List Head to Tail :  2  3  6  9  12  14  16  25
Linked List Tail to Head :  25  16  14  12  9  6  3  2




Comment

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