Find largest and smallest elements in linked list

Here given code implementation process.

// C Program
// Find largest and smallest elements in linked list
#include <stdio.h>
 //For malloc function
#include <stdlib.h>

//Linked List Node
struct Node
{
	int data;
	struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
	//Create dynamic node
	struct Node *node = (struct Node *) malloc(sizeof(struct Node));
	if (node == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		//Set initial node value
		node->data = data;
		node->next = NULL;
	}
	return node;
}
//Add new node at end of linked list 
void insert(struct Node **head, int data)
{
	struct Node *node = create_node(data);
	if ( *head == NULL)
	{
		*head = node;
	}
	else
	{
		struct Node *temp = *head;
		//Find last node
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		//Add node at last possition
		temp->next = node;
	}
}
//Display linked list element
void display(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked list\n");
		return;
	}
	struct Node *temp = head;
	printf("\n Linked List : ");
	//iterating linked list elements
	while (temp != NULL)
	{
		printf("  %d", temp->data);
		//visit to next node
		temp = temp->next;
	}
}
//Find the largest and smallest element in linked list
void largest_smallest(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked list\n");
		return;
	}
	//Display linked list
	display(head);
	//Define some auxiliary variables
	struct Node *big = NULL;
	struct Node *small = NULL;
	struct Node *temp = head;
	//iterating linked list elements
	while (temp != NULL)
	{
		if (big == NULL)
		{
			//Get first node
			big = temp;
		}
		else if (big->data < temp->data)
		{
			if (small == NULL)
			{
				small = big;
			}
			//Get current largest node
			big = temp;
		}
		else if (big->data != temp->data)
		{
			if (small == NULL || small->data > temp->data)
			{
				//get new smallest
				small = temp;
			}
		}
		//visit to next node
		temp = temp->next;
	}
	if (big != NULL)
	{
		printf("\n Largest  :  %d\n", big->data);
	}
	if (small != NULL)
	{
		printf(" Smallest : %d \n", small->data);
	}
	else
	{
		//When smallest node are not exist in linked list
		printf(" Smallest  : None\n");
	}
}
int main()
{
	//Define few empty  linked list
	struct Node *list1 = NULL;
	struct Node *list2 = NULL;
	struct Node *list3 = NULL;
	struct Node *list4 = NULL;
	//Add node in first linked list
	insert( &list1, 6);
	insert( &list1, 4);
	insert( &list1, 5);
	insert( &list1, 10);
	insert( &list1, 3);
	insert( &list1, 7);
	insert( &list1, 9);
	insert( &list1, 2);
	insert( &list1, 8);
	largest_smallest(list1);
	//Add node in second linked list
	insert( &list2, 1);
	insert( &list2, -3);
	insert( &list2, 4);
	insert( &list2, 2);
	insert( &list2, 3);
	insert( &list2, 1);
	largest_smallest(list2);
	//Add node in third linked list
	insert( &list3, 2);
	insert( &list3, 1);
	insert( &list3, 8);
	insert( &list3, 4);
	insert( &list3, 8);
	insert( &list3, 1);
	insert( &list3, 3);
	insert( &list3, 2);
	largest_smallest(list3);
	//Add node in fourth linked list
	insert( &list4, 2);
	insert( &list4, 2);
	insert( &list4, 2);
	largest_smallest(list4);
	return 0;
}

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest  :  10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest  :  4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest  :  8
 Smallest : 1

 Linked List :   2  2  2
 Largest  :  2
 Smallest  : None
// Java Program
// Find largest and smallest elements in linked list

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
public class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		System.out.print("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			//display node value
			System.out.print("  " + temp.data);
			//visit to next node
			temp = temp.next;
		}
	}
	//Find the largest and smallest element in linked list
	public void largest_smallest()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked list\n");
			return;
		}
		//Display linked list
		this.display();
		//Define some auxiliary variables
		Node big = null;
		Node small = null;
		Node temp = head;
		//iterating linked list elements
		while (temp != null)
		{
			if (big == null)
			{
				//Get first node
				big = temp;
			}
			else if (big.data < temp.data)
			{
				if (small == null)
				{
					small = big;
				}
				//Get current largest node
				big = temp;
			}
			else if (big.data != temp.data)
			{
				if (small == null || small.data > temp.data)
				{
					//get new smallest
					small = temp;
				}
			}
			//visit to next node
			temp = temp.next;
		}
		if (big != null)
		{
			System.out.print("\n Largest : " + big.data + "\n");
		}
		if (small != null)
		{
			System.out.print(" Smallest : " + small.data + " \n");
		}
		else
		{
			//When smallest node are not exist in linked list
			System.out.print(" Smallest : None\n");
		}
	}
	public static void main(String[] args)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		MyLinkedList list3 = new MyLinkedList();
		MyLinkedList list4 = new MyLinkedList();
		//Add node in first linked list
		list1.insert(6);
		list1.insert(4);
		list1.insert(5);
		list1.insert(10);
		list1.insert(3);
		list1.insert(7);
		list1.insert(9);
		list1.insert(2);
		list1.insert(8);
		list1.largest_smallest();
		//Add node in second linked list
		list2.insert(1);
		list2.insert(-3);
		list2.insert(4);
		list2.insert(2);
		list2.insert(3);
		list2.insert(1);
		list2.largest_smallest();
		//Add node in third linked list
		list3.insert(2);
		list3.insert(1);
		list3.insert(8);
		list3.insert(4);
		list3.insert(8);
		list3.insert(1);
		list3.insert(3);
		list3.insert(2);
		list3.largest_smallest();
		//Add node in fourth linked list
		list4.insert(2);
		list4.insert(2);
		list4.insert(2);
		list4.largest_smallest();
	}
}

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Find largest and smallest elements in linked list

//Node of LinkedList
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: Node * head;
	Node * tail;
	//Class constructor
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void insert(int data)
	{
		//Create a node
		Node * node = new Node(data);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked list\n";
			return;
		}
		Node * temp = this->head;
		cout << "\n Linked List : ";
		//iterating linked list elements
		while (temp != NULL)
		{
			//display node value
			cout << "  " << temp->data;
			//visit to next node
			temp = temp->next;
		}
	}
	//Find the largest and smallest element in linked list
	void largest_smallest()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked list\n";
			return;
		}
		//Display linked list
		this->display();
		//Define some auxiliary variables
		Node * big = NULL;
		Node * small = NULL;
		Node * temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			if (big == NULL)
			{
				//Get first node
				big = temp;
			}
			else if (big->data < temp->data)
			{
				if (small == NULL)
				{
					small = big;
				}
				//Get current largest node
				big = temp;
			}
			else if (big->data != temp->data)
			{
				if (small == NULL || small->data > temp->data)
				{
					//get new smallest
					small = temp;
				}
			}
			//visit to next node
			temp = temp->next;
		}
		if (big != NULL)
		{
			cout << "\n Largest : " << big->data << "\n";
		}
		if (small != NULL)
		{
			cout << " Smallest : " << small->data << " \n";
		}
		else
		{
			//When smallest node are not exist in linked list
			cout << " Smallest : None\n";
		}
	}
};
int main()
{
	MyLinkedList list1 = MyLinkedList();
	MyLinkedList list2 = MyLinkedList();
	MyLinkedList list3 = MyLinkedList();
	MyLinkedList list4 = MyLinkedList();
    //Add node in first linked list
    list1.insert(6);
    list1.insert(4);
    list1.insert(5);
    list1.insert(10);
    list1.insert(3);
    list1.insert(7);
    list1.insert(9);
    list1.insert(2);
    list1.insert(8);
    list1.largest_smallest();
    //Add node in second linked list
    list2.insert(1);
    list2.insert(-3);
    list2.insert(4);
    list2.insert(2);
    list2.insert(3);
    list2.insert(1);
    list2.largest_smallest();
    //Add node in third linked list
    list3.insert(2);
    list3.insert(1);
    list3.insert(8);
    list3.insert(4);
    list3.insert(8);
    list3.insert(1);
    list3.insert(3);
    list3.insert(2);
    list3.largest_smallest();
    //Add node in fourth linked list
    list4.insert(2);
    list4.insert(2);
    list4.insert(2);
    list4.largest_smallest();
	return 0;
}

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
//Include namespace system
using System;
// C# Program
// Find largest and smallest elements in linked list

//Node of LinkedList
public class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
public class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		Console.Write("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			//display node value
			Console.Write("  " + temp.data);
			//visit to next node
			temp = temp.next;
		}
	}
	//Find the largest and smallest element in linked list
	public void largest_smallest()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked list\n");
			return;
		}
		//Display linked list
		this.display();
		//Define some auxiliary variables
		Node big = null;
		Node small = null;
		Node temp = head;
		//iterating linked list elements
		while (temp != null)
		{
			if (big == null)
			{
				//Get first node
				big = temp;
			}
			else if (big.data < temp.data)
			{
				if (small == null)
				{
					small = big;
				}
				//Get current largest node
				big = temp;
			}
			else if (big.data != temp.data)
			{
				if (small == null || small.data > temp.data)
				{
					//get new smallest
					small = temp;
				}
			}
			//visit to next node
			temp = temp.next;
		}
		if (big != null)
		{
			Console.Write("\n Largest : " + big.data + "\n");
		}
		if (small != null)
		{
			Console.Write(" Smallest : " + small.data + " \n");
		}
		else
		{
			//When smallest node are not exist in linked list
			Console.Write(" Smallest : None\n");
		}
	}
	public static void Main(String[] args)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		MyLinkedList list3 = new MyLinkedList();
		MyLinkedList list4 = new MyLinkedList();
		//Add node in first linked list
		list1.insert(6);
		list1.insert(4);
		list1.insert(5);
		list1.insert(10);
		list1.insert(3);
		list1.insert(7);
		list1.insert(9);
		list1.insert(2);
		list1.insert(8);
		list1.largest_smallest();
		//Add node in second linked list
		list2.insert(1);
		list2.insert(-3);
		list2.insert(4);
		list2.insert(2);
		list2.insert(3);
		list2.insert(1);
		list2.largest_smallest();
		//Add node in third linked list
		list3.insert(2);
		list3.insert(1);
		list3.insert(8);
		list3.insert(4);
		list3.insert(8);
		list3.insert(1);
		list3.insert(3);
		list3.insert(2);
		list3.largest_smallest();
		//Add node in fourth linked list
		list4.insert(2);
		list4.insert(2);
		list4.insert(2);
		list4.largest_smallest();
	}
}

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
<?php
// Php Program
// Find largest and smallest elements in linked list

//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructor
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function insert($data)
	{
		//Create a node
		$node = new Node($data);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked list\n";
			return;
		}
		$temp = $this->head;
		echo "\n Linked List : ";
		//iterating linked list elements
		while ($temp != null)
		{
			//display node value
			echo "  ". $temp->data;
			//visit to next node
			$temp = $temp->next;
		}
	}
	//Find the largest and smallest element in linked list
	public	function largest_smallest()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked list\n";
			return;
		}
		//Display linked list
		$this->display();
		//Define some auxiliary variables
		$big = null;
		$small = null;
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			if ($big == null)
			{
				//Get first node
				$big = $temp;
			}
			else if ($big->data < $temp->data)
			{
				if ($small == null)
				{
					$small = $big;
				}
				//Get current largest node
				$big = $temp;
			}
			else if ($big->data != $temp->data)
			{
				if ($small == null || $small->data > $temp->data)
				{
					//get new smallest
					$small = $temp;
				}
			}
			//visit to next node
			$temp = $temp->next;
		}
		if ($big != null)
		{
			echo "\n Largest : ". $big->data ."\n";
		}
		if ($small != null)
		{
			echo " Smallest : ". $small->data ." \n";
		}
		else
		{
			//When smallest node are not exist in linked list
			echo " Smallest : None\n";
		}
	}
}

function main()
{
	$list1 = new MyLinkedList();
	$list2 = new MyLinkedList();
	$list3 = new MyLinkedList();
	$list4 = new MyLinkedList();
	//Add node in first linked list
	$list1->insert(6);
	$list1->insert(4);
	$list1->insert(5);
	$list1->insert(10);
	$list1->insert(3);
	$list1->insert(7);
	$list1->insert(9);
	$list1->insert(2);
	$list1->insert(8);
	$list1->largest_smallest();
	//Add node in second linked list
	$list2->insert(1);
	$list2->insert(-3);
	$list2->insert(4);
	$list2->insert(2);
	$list2->insert(3);
	$list2->insert(1);
	$list2->largest_smallest();
	//Add node in third linked list
	$list3->insert(2);
	$list3->insert(1);
	$list3->insert(8);
	$list3->insert(4);
	$list3->insert(8);
	$list3->insert(1);
	$list3->insert(3);
	$list3->insert(2);
	$list3->largest_smallest();
	//Add node in fourth linked list
	$list4->insert(2);
	$list4->insert(2);
	$list4->insert(2);
	$list4->largest_smallest();
}
main();

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
// Node Js Program
// Find largest and smallest elements in linked list

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructor
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	insert(data)
	{
		//Create a node
		var node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		var temp = this.head;
		process.stdout.write("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			//display node value
			process.stdout.write("  " + temp.data);
			//visit to next node
			temp = temp.next;
		}
	}
	//Find the largest and smallest element in linked list
	largest_smallest()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		//Display linked list
		this.display();
		//Define some auxiliary variables
		var big = null;
		var small = null;
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (big == null)
			{
				//Get first node
				big = temp;
			}
			else if (big.data < temp.data)
			{
				if (small == null)
				{
					small = big;
				}
				//Get current largest node
				big = temp;
			}
			else if (big.data != temp.data)
			{
				if (small == null || small.data > temp.data)
				{
					//get new smallest
					small = temp;
				}
			}
			//visit to next node
			temp = temp.next;
		}
		if (big != null)
		{
			process.stdout.write("\n Largest : " + big.data + "\n");
		}
		if (small != null)
		{
			process.stdout.write(" Smallest : " + small.data + " \n");
		}
		else
		{
			//When smallest node are not exist in linked list
			process.stdout.write(" Smallest : None\n");
		}
	}
}

function main()
{
	var list1 = new MyLinkedList();
	var list2 = new MyLinkedList();
	var list3 = new MyLinkedList();
	var list4 = new MyLinkedList();
	//Add node in first linked list
	list1.insert(6);
	list1.insert(4);
	list1.insert(5);
	list1.insert(10);
	list1.insert(3);
	list1.insert(7);
	list1.insert(9);
	list1.insert(2);
	list1.insert(8);
	list1.largest_smallest();
	//Add node in second linked list
	list2.insert(1);
	list2.insert(-3);
	list2.insert(4);
	list2.insert(2);
	list2.insert(3);
	list2.insert(1);
	list2.largest_smallest();
	//Add node in third linked list
	list3.insert(2);
	list3.insert(1);
	list3.insert(8);
	list3.insert(4);
	list3.insert(8);
	list3.insert(1);
	list3.insert(3);
	list3.insert(2);
	list3.largest_smallest();
	//Add node in fourth linked list
	list4.insert(2);
	list4.insert(2);
	list4.insert(2);
	list4.largest_smallest();
}
main();

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
#  Python 3 Program
#  Find largest and smallest elements in linked list

# Node of LinkedList
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		self.next = None
	

class MyLinkedList :
	
	# Class constructor
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def insert(self, data) :
		# Create a node
		node = Node(data)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\nEmpty linked list\n", end = "")
			return
		
		temp = self.head
		print("\n Linked List : ", end = "")
		# iterating linked list elements
		while (temp != None) :
			# display node value
			print("  ", temp.data, end = "")
			# visit to next node
			temp = temp.next
		
	
	# Find the largest and smallest element in linked list
	def largest_smallest(self) :
		if (self.head == None) :
			print("\nEmpty linked list\n", end = "")
			return
		
		# Display linked list
		self.display()
		# Define some auxiliary variables
		big = None
		small = None
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			if (big == None) :
				# Get first node
				big = temp
			
			elif(big.data < temp.data) :
				if (small == None) :
					small = big
				
				# Get current largest node
				big = temp
			
			elif(big.data != temp.data) :
				if (small == None or small.data > temp.data) :
					# get new smallest
					small = temp
				
			
			# visit to next node
			temp = temp.next
		
		if (big != None) :
			print("\n Largest : ", big.data ,"\n", end = "")
		
		if (small != None) :
			print(" Smallest : ", small.data ," \n", end = "")
		else :
			# When smallest node are not exist in linked list
			print(" Smallest : None\n", end = "")
		
	

def main() :
	list1 = MyLinkedList()
	list2 = MyLinkedList()
	list3 = MyLinkedList()
	list4 = MyLinkedList()
	# Add node in first linked list
	list1.insert(6)
	list1.insert(4)
	list1.insert(5)
	list1.insert(10)
	list1.insert(3)
	list1.insert(7)
	list1.insert(9)
	list1.insert(2)
	list1.insert(8)
	list1.largest_smallest()
	# Add node in second linked list
	list2.insert(1)
	list2.insert(-3)
	list2.insert(4)
	list2.insert(2)
	list2.insert(3)
	list2.insert(1)
	list2.largest_smallest()
	# Add node in third linked list
	list3.insert(2)
	list3.insert(1)
	list3.insert(8)
	list3.insert(4)
	list3.insert(8)
	list3.insert(1)
	list3.insert(3)
	list3.insert(2)
	list3.largest_smallest()
	# Add node in fourth linked list
	list4.insert(2)
	list4.insert(2)
	list4.insert(2)
	list4.largest_smallest()

if __name__ == "__main__": main()

Output

 Linked List :    6   4   5   10   3   7   9   2   8
 Largest :  10
 Smallest :  2

 Linked List :    1   -3   4   2   3   1
 Largest :  4
 Smallest :  -3

 Linked List :    2   1   8   4   8   1   3   2
 Largest :  8
 Smallest :  1

 Linked List :    2   2   2
 Largest :  2
 Smallest : None
#  Ruby Program
#  Find largest and smallest elements in linked list

# Node of LinkedList
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :next
	attr_accessor :data, :next
    
	def initialize(data)
	
		# Set node value
		self.data = data
		self.next = nil
	end
end
class MyLinkedList 

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail

	# Class constructor
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def insert(data)
	
		# Create a node
		node = Node.new(data)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Display linked list element
	def display()
	
		if (self.head == nil)
		
			print("\nEmpty linked list\n")
			return
		end
		temp = self.head
		print("\n Linked List : ")
		# iterating linked list elements
		while (temp != nil)
		
			# display node value
			print("  ", temp.data)
			# visit to next node
			temp = temp.next
		end
	end
	# Find the largest and smallest element in linked list
	def largest_smallest()
	
		if (self.head == nil)
		
			print("\nEmpty linked list\n")
			return
		end
		# Display linked list
		self.display()
		# Define some auxiliary variables
		big = nil
		small = nil
		temp = @head
		# iterating linked list elements
		while (temp != nil)
		
			if (big == nil)
			
				# Get first node
				big = temp
			elsif(big.data < temp.data)
			
				if (small == nil)
				
					small = big
				end
				# Get current largest node
				big = temp
			elsif(big.data != temp.data)
			
				if (small == nil || small.data > temp.data)
				
					# get new smallest
					small = temp
				end
			end
			# visit to next node
			temp = temp.next
		end
		if (big != nil)
		
			print("\n Largest : ", big.data ,"\n")
		end
		if (small != nil)
		
			print(" Smallest : ", small.data ," \n")
		else
		
			# When smallest node are not exist in linked list
			print(" Smallest : None\n")
		end
	end
end
def main()

	list1 = MyLinkedList.new()
	list2 = MyLinkedList.new()
	list3 = MyLinkedList.new()
	list4 = MyLinkedList.new()
	# Add node in first linked list
	list1.insert(6)
	list1.insert(4)
	list1.insert(5)
	list1.insert(10)
	list1.insert(3)
	list1.insert(7)
	list1.insert(9)
	list1.insert(2)
	list1.insert(8)
	list1.largest_smallest()
	# Add node in second linked list
	list2.insert(1)
	list2.insert(-3)
	list2.insert(4)
	list2.insert(2)
	list2.insert(3)
	list2.insert(1)
	list2.largest_smallest()
	# Add node in third linked list
	list3.insert(2)
	list3.insert(1)
	list3.insert(8)
	list3.insert(4)
	list3.insert(8)
	list3.insert(1)
	list3.insert(3)
	list3.insert(2)
	list3.largest_smallest()
	# Add node in fourth linked list
	list4.insert(2)
	list4.insert(2)
	list4.insert(2)
	list4.largest_smallest()
end
main()

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2 

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3 

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1 

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
// Scala Program
// Find largest and smallest elements in linked list

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	var tail: Node)
{
	//Class constructor
	def this()
	{
		this(null, null);
	}
	//insert node at last of linke list
	def insert(data: Int): Unit = {
		//Create a node
		var node: Node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\nEmpty linked list\n");
			return;
		}
		var temp: Node = this.head;
		print("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			//display node value
			print("  " + temp.data);
			//visit to next node
			temp = temp.next;
		}
	}
	//Find the largest and smallest element in linked list
	def largest_smallest(): Unit = {
		if (this.head == null)
		{
			print("\nEmpty linked list\n");
			return;
		}
		//Display linked list
		this.display();
		//Define some auxiliary variables
		var big: Node = null;
		var small: Node = null;
		var temp: Node = head;
		//iterating linked list elements
		while (temp != null)
		{
			if (big == null)
			{
				//Get first node
				big = temp;
			}
			else if (big.data < temp.data)
			{
				if (small == null)
				{
					small = big;
				}
				//Get current largest node
				big = temp;
			}
			else if (big.data != temp.data)
			{
				if (small == null || small.data > temp.data)
				{
					//get new smallest
					small = temp;
				}
			}
			//visit to next node
			temp = temp.next;
		}
		if (big != null)
		{
			print("\n Largest : " + big.data + "\n");
		}
		if (small != null)
		{
			print(" Smallest : " + small.data + " \n");
		}
		else
		{
			//When smallest node are not exist in linked list
			print(" Smallest : None\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var list1: MyLinkedList = new MyLinkedList();
		var list2: MyLinkedList = new MyLinkedList();
		var list3: MyLinkedList = new MyLinkedList();
		var list4: MyLinkedList = new MyLinkedList();
		//Add node in first linked list
		list1.insert(6);
		list1.insert(4);
		list1.insert(5);
		list1.insert(10);
		list1.insert(3);
		list1.insert(7);
		list1.insert(9);
		list1.insert(2);
		list1.insert(8);
		list1.largest_smallest();
		//Add node in second linked list
		list2.insert(1);
		list2.insert(-3);
		list2.insert(4);
		list2.insert(2);
		list2.insert(3);
		list2.insert(1);
		list2.largest_smallest();
		//Add node in third linked list
		list3.insert(2);
		list3.insert(1);
		list3.insert(8);
		list3.insert(4);
		list3.insert(8);
		list3.insert(1);
		list3.insert(3);
		list3.insert(2);
		list3.largest_smallest();
		//Add node in fourth linked list
		list4.insert(2);
		list4.insert(2);
		list4.insert(2);
		list4.largest_smallest();
	}
}

Output

 Linked List :   6  4  5  10  3  7  9  2  8
 Largest : 10
 Smallest : 2

 Linked List :   1  -3  4  2  3  1
 Largest : 4
 Smallest : -3

 Linked List :   2  1  8  4  8  1  3  2
 Largest : 8
 Smallest : 1

 Linked List :   2  2  2
 Largest : 2
 Smallest : None
// Swift Program
// Find largest and smallest elements in linked list

//Node of LinkedList
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		//Set node value
		self.data = data;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructor
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//insert node at last of linke list
	func insert(_ data: Int)
	{
		//Create a node
		let node: Node? = Node(data);
		if (self.head == nil)
		{
			//When linked list empty add first node
			self.head = node;
			self.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			self.tail!.next = node;
			self.tail = node;
		}
	}
	//Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\nEmpty linked list\n", terminator: "");
			return;
		}
		var temp: Node? = self.head;
		print("\n Linked List : ", terminator: "");
		//iterating linked list elements
		while (temp != nil)
		{
			//display node value
			print("  ", temp!.data, terminator: "");
			//visit to next node
			temp = temp!.next;
		}
	}
	//Find the largest and smallest element in linked list
	func largest_smallest()
	{
		if (self.head == nil)
		{
			print("\nEmpty linked list\n", terminator: "");
			return;
		}
		//Display linked list
		self.display();
		//Define some auxiliary variables
		var big: Node? = nil;
		var small: Node? = nil;
		var temp: Node? = self.head;
		//iterating linked list elements
		while (temp != nil)
		{
			if (big == nil)
			{
				//Get first node
				big = temp;
			}
			else if (big!.data < temp!.data)
			{
				if (small == nil)
				{
					small = big;
				}
				//Get current largest node
				big = temp;
			}
			else if (big!.data != temp!.data)
			{
				if (small == nil || small!.data > temp!.data)
				{
					//get new smallest
					small = temp;
				}
			}
			//visit to next node
			temp = temp!.next;
		}
		if (big != nil)
		{
			print("\n Largest : ", big!.data ,"\n", terminator: "");
		}
		if (small != nil)
		{
			print(" Smallest : ", small!.data ," \n", terminator: "");
		}
		else
		{
			//When smallest node are not exist in linked list
			print(" Smallest : None\n", terminator: "");
		}
	}
}
func main()
{
	let list1: MyLinkedList = MyLinkedList();
	let list2: MyLinkedList = MyLinkedList();
	let list3: MyLinkedList = MyLinkedList();
	let list4: MyLinkedList = MyLinkedList();
  
    //Add node in first linked list
    list1.insert(6);
    list1.insert(4);
    list1.insert(5);
    list1.insert(10);
    list1.insert(3);
    list1.insert(7);
    list1.insert(9);
    list1.insert(2);
    list1.insert(8);
    list1.largest_smallest();
    //Add node in second linked list
    list2.insert(1);
    list2.insert(-3);
    list2.insert(4);
    list2.insert(2);
    list2.insert(3);
    list2.insert(1);
    list2.largest_smallest();
    //Add node in third linked list
    list3.insert(2);
    list3.insert(1);
    list3.insert(8);
    list3.insert(4);
    list3.insert(8);
    list3.insert(1);
    list3.insert(3);
    list3.insert(2);
    list3.largest_smallest();
    //Add node in fourth linked list
    list4.insert(2);
    list4.insert(2);
    list4.insert(2);
    list4.largest_smallest();
}
main();

Output

 Linked List :    6   4   5   10   3   7   9   2   8
 Largest :  10
 Smallest :  2

 Linked List :    1   -3   4   2   3   1
 Largest :  4
 Smallest :  -3

 Linked List :    2   1   8   4   8   1   3   2
 Largest :  8
 Smallest :  1

 Linked List :    2   2   2
 Largest :  2
 Smallest : None


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







© 2021, kalkicode.com, All rights reserved