Skip to main content

Find the smallest number in the doubly linked list in typescript

Ts program for Find the smallest number in the doubly linked list. Here problem description and explanation.

// TypeScript program for
// Find the smallest node in doubly linked list

// Linked List Node
class LinkNode
{
	public data: number;
	public next: LinkNode;
	public prev: LinkNode;
	constructor(data: number)
	{
		this.data = data;
		this.next = null;
		this.prev = null;
	}
}
class DoublyLinkedList
{
	public head: LinkNode;
	public tail: LinkNode;
	constructor()
	{
		// Set inital value
		this.head = null;
		this.tail = null;
	}
	// Insert new node at end position
	public insert(value: number)
	{
		// 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;
		// new last node
		this.tail = node;
	}
	// Find smallest number
	public minNumber()
	{
		if (this.head == null)
		{
			console.log("Empty Linked List");
		}
		else
		{
			// Get first node of linked list
			var temp = this.head;
			var result = temp.data;
			// iterate linked list 
			while (temp != null)
			{
				if (temp.data < result)
				{
					// Get new minimum
					result = temp.data;
				}
				// Visit to next node
				temp = temp.next;
			}
			console.log("Smallest : " + result);
		}
	}
	public static main(args: string[])
	{
		var dll = new DoublyLinkedList();
		// Insert following linked list nodes
		dll.insert(14);
		dll.insert(31);
		dll.insert(12);
		dll.insert(15);
		dll.insert(11);
		dll.insert(25);
		dll.minNumber();
	}
}
DoublyLinkedList.main([]);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

Smallest : 11




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