Skip to main content

Find the smallest number in the doubly linked list in scala

Scala program for Find the smallest number in the doubly linked list. Here more information.

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

// Linked List Node
class LinkNode(var data: Int,
	var next: LinkNode,
		var prev: LinkNode)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class DoublyLinkedList(var head: LinkNode,
	var tail: LinkNode)
{
	def this()
	{
		this(null, null);
	}
	// Insert new node at end position
	def insert(value: Int): Unit = {
		// Create a node
		var node: LinkNode = 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
	def minNumber(): Unit = {
		if (this.head == null)
		{
			println("Empty Linked List");
		}
		else
		{
			// Get first node of linked list
			var temp: LinkNode = this.head;
			var result: Int = 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;
			}
			println("Smallest : " + result);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var dll: DoublyLinkedList = 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();
	}
}

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