Count minimum frequency elements in a linked list

Here given code implementation process.

// C Program
// Count minimum frequency elements in a linked list
#include <stdio.h>
 //For malloc function
#include <stdlib.h>

//Linked List Node
struct Node
{
    int data;
    struct Node *next;
};
//Frequency node counter
struct NodeCounter
{
    int data;
    int frequency;
    struct NodeCounter *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;
    }
    printf("\n");
}
//Create a new node of NodeCounter
struct NodeCounter *make_counter(int data)
{
    //Create dynamic node of node counter
    struct NodeCounter *node = (struct NodeCounter *) malloc(sizeof(struct NodeCounter));
    if (node == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        //Set initial node value
        node->data = data;
        //Set the initial node frequency
        node->frequency = 1;
        node->next = NULL;
    }
    return node;
}
//Find node of given key in auxiliary list
struct NodeCounter *find_node(struct NodeCounter *front, int key)
{
    struct NodeCounter *temp = front;
    while (temp != NULL)
    {
        if (temp->data == key)
        {
            //When key node exist
            return temp;
        }
        //visit to next node
        temp = temp->next;
    }
    //when key not exist
    return NULL;
}
//Count all nodes which containing of minimum frequency
void min_frequency_node(struct Node *head)
{
    if (head == NULL)
    {
        return;
    }
    //Display of linked list nodes
    display(head);
    //Define some auxiliary variables
    struct NodeCounter *front = NULL;
    struct NodeCounter *tail = NULL;
    struct NodeCounter *current = NULL;
    struct NodeCounter *temp = NULL;
    //Resultant variables
    int counter = 1;
    //Get list1 node of linked list
    struct Node *auxiliary = head;
    //Create auxiliary list which is contain data and element occurrence
    while (auxiliary != NULL)
    {
        //Find auxiliary node value in resultant list
        current = find_node(front, auxiliary->data);
        if (current != NULL)
        {
            //update node frequency
            current->frequency = current->frequency + 1;
        }
        else
        {
            //create new node of auxiliary list
            current = make_counter(auxiliary->data);
            if (front == NULL)
            {
                //list1 node of auxiliary list
                front = current;
            }
            else
            {
                //add new node at end of auxiliary list
                tail->next = current;
            }
            tail = current;
        }
        //visit to next node
        auxiliary = auxiliary->next;
    }
    //Start to list1 node
    temp = front;
    current = front;
    //Find minimum frequency
    while (temp != NULL)
    {
        if (current->data > temp->data)
        {
            //Get node of minimum frequency
            current = temp;
        }
        //visit to next node
        temp = temp->next;
    }
    if (current == NULL)
    {
        //When auxiliary list are empty
        printf(" None ");
    }
    else
    {
        //Display the value of resultant minimum frequency
        printf(" Result Minimum Frequency : %d \n Nodes : [", current->frequency);
        //reset value
        counter = 0;
        tail = NULL;
        //Count nodes which is contain minimum frequency
        while (front != NULL)
        {
            //Get current node
            temp = front;
            if (front->frequency == current->frequency)
            {
                //print node of which is contain minimum frequency
                printf(" %d", front->data);
                //count minimum frequency element 
                counter++;
            }
            //visit to next node
            front = front->next;
            //free node
            free(temp);
            temp = NULL;
        }
        //Display result
        printf(" ]\n Result : %d \n", counter);
    }
    printf("\n");
}
int main()
{
    struct Node *list1 = NULL;
    //Create first linked list
    insert( &list1, 2);
    insert( &list1, 4);
    insert( &list1, 7);
    insert( &list1, 5);
    insert( &list1, 7);
    insert( &list1, 7);
    insert( &list1, 4);
    insert( &list1, 1);
    insert( &list1, 5);
    insert( &list1, 3);
    insert( &list1, 1);
    insert( &list1, 2);
    insert( &list1, 4);
    min_frequency_node(list1);
    struct Node *list2 = NULL;
    //Create second linked list
    insert( &list2, 9);
    insert( &list2, 5);
    insert( &list2, 7);
    insert( &list2, 4);
    insert( &list2, 7);
    insert( &list2, 7);
    insert( &list2, 5);
    min_frequency_node(list2);
    return 0;
}

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
// Java Program
// Count minimum frequency elements in a 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;
	}
}
//Frequency node counter
class NodeCounter
{
	public int data;
	public int frequency;
	public NodeCounter next;
	public NodeCounter(int data)
	{
		//Set initial node value
		this.data = data;
		//Set the initial node frequency
		this.frequency = 1;
		this.next = null;
	}
};
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	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;
		}
		System.out.print("\n");
	}
	//Find node of given key in list
	public NodeCounter find_node(NodeCounter front, int key)
	{
		NodeCounter temp = front;
		while (temp != null)
		{
			if (temp.data == key)
			{
				//When key node exist
				return temp;
			}
			//visit to next node
			temp = temp.next;
		}
		//when key not exist
		return null;
	}
	//Count all nodes which containing of minimum frequency
	public void min_frequency_node()
	{
		if (this.head == null)
		{
			return;
		}
		//Display of linked list nodes
		this.display();
		//Define some auxiliary variables
		NodeCounter front = new NodeCounter(this.head.data);
		NodeCounter last = front;
		NodeCounter current = null;
		NodeCounter temp = null;
		//Resultant variables
		int counter = 1;
		//Get second node of linked list
		Node auxiliary = this.head.next;
		//Create auxiliary list which is contain data and element occurrence
		while (auxiliary != null)
		{
			//Find auxiliary node value in resultant list
			current = find_node(front, auxiliary.data);
			if (current != null)
			{
				//update node frequency
				current.frequency = current.frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				current = new NodeCounter(auxiliary.data);
				//add new node at end of auxiliary list
				last.next = current;
				last = current;
			}
			//visit to next node
			auxiliary = auxiliary.next;
		}
		//Start to first node
		temp = front;
		current = front;
		//Find minimum frequency
		while (temp != null)
		{
			if (current.data > temp.data)
			{
				//Get node of minimum frequency
				current = temp;
			}
			//visit to next node
			temp = temp.next;
		}
		if (current == null)
		{
			//When auxiliary list are empty
			System.out.print(" None ");
		}
		else
		{
			//Display the value of resultant minimum frequency
			System.out.print(" Result Minimum Frequency : " + current.frequency + " \n Nodes : [");
			//reset value
			counter = 0;
			last = null;
			//Count nodes which is contain minimum frequency
			while (front != null)
			{
				//Get current node
				temp = front;
				if (front.frequency == current.frequency)
				{
					//print node of which is contain minimum frequency
					System.out.print(" " + front.data);
					//count minimum frequency element 
					counter++;
				}
				//visit to next node
				front = front.next;
				//free node
				temp.next = null;
				temp = null;
			}
			//Display result
			System.out.print(" ]\n Result : " + counter + " \n");
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		//Create first linked list
		list1.insert(2);
		list1.insert(4);
		list1.insert(7);
		list1.insert(5);
		list1.insert(7);
		list1.insert(7);
		list1.insert(4);
		list1.insert(1);
		list1.insert(5);
		list1.insert(3);
		list1.insert(1);
		list1.insert(2);
		list1.insert(4);
		list1.min_frequency_node();
		//Create second linked list
		list2.insert(9);
		list2.insert(5);
		list2.insert(7);
		list2.insert(4);
		list2.insert(7);
		list2.insert(7);
		list2.insert(5);
		list2.min_frequency_node();
	}
}

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Count minimum frequency elements in a linked list

//Node of LinkedList
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->next = NULL;
	}
};
//Frequency node counter
class NodeCounter
{
	public: int data;
	int frequency;
	NodeCounter * next;
	NodeCounter(int data)
	{
		//Set initial node value
		this->data = data;
		//Set the initial node frequency
		this->frequency = 1;
		this->next = NULL;
	}
};;
class MyLinkedList
{
	public: Node * head;
	Node * tail;
	//Class constructors
	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;
		}
		cout << "\n";
	}
	//Find node of given key in list
	NodeCounter * find_node(NodeCounter * front, int key)
	{
		NodeCounter * temp = front;
		while (temp != NULL)
		{
			if (temp->data == key)
			{
				//When key node exist
				return temp;
			}
			//visit to next node
			temp = temp->next;
		}
		//when key not exist
		return NULL;
	}
	//Count all nodes which containing of minimum frequency
	void min_frequency_node()
	{
		if (this->head == NULL)
		{
			return;
		}
		//Display of linked list nodes
		this->display();
		//Define some auxiliary variables
		NodeCounter * front = new NodeCounter(this->head->data);
		NodeCounter * last = front;
		NodeCounter * current = NULL;
		NodeCounter * temp = NULL;
		//Resultant variables
		int counter = 1;
		//Get second node of linked list
		Node * auxiliary = this->head->next;
		//Create auxiliary list which is contain data and element occurrence
		while (auxiliary != NULL)
		{
			//Find auxiliary node value in resultant list
			current = this->find_node(front, auxiliary->data);
			if (current != NULL)
			{
				//update node frequency
				current->frequency = current->frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				current = new NodeCounter(auxiliary->data);
				//add new node at end of auxiliary list
				last->next = current;
				last = current;
			}
			//visit to next node
			auxiliary = auxiliary->next;
		}
		//Start to first node
		temp = front;
		current = front;
		//Find minimum frequency
		while (temp != NULL)
		{
			if (current->data > temp->data)
			{
				//Get node of minimum frequency
				current = temp;
			}
			//visit to next node
			temp = temp->next;
		}
		if (current == NULL)
		{
			//When auxiliary list are empty
			cout << " None ";
		}
		else
		{
			//Display the value of resultant minimum frequency
			cout << " Result Minimum Frequency : " << current->frequency << " \n Nodes : [";
			//reset value
			counter = 0;
			last = NULL;
			//Count nodes which is contain minimum frequency
			while (front != NULL)
			{
				//Get current node
				temp = front;
				if (front->frequency == current->frequency)
				{
					//print node of which is contain minimum frequency
					cout << " " << front->data;
					//count minimum frequency element 
					counter++;
				}
				//visit to next node
				front = front->next;
				//free node
				temp->next = NULL;
				temp = NULL;
			}
			//Display result
			cout << " ]\n Result : " << counter << " \n";
		}
		cout << "\n";
	}
};
int main()
{
	MyLinkedList list1 = MyLinkedList();
	MyLinkedList list2 = MyLinkedList();

    //Create first linked list
    list1.insert(2);
    list1.insert(4);
    list1.insert(7);
    list1.insert(5);
    list1.insert(7);
    list1.insert(7);
    list1.insert(4);
    list1.insert(1);
    list1.insert(5);
    list1.insert(3);
    list1.insert(1);
    list1.insert(2);
    list1.insert(4);
    list1.min_frequency_node();
    //Create second linked list
    list2.insert(9);
    list2.insert(5);
    list2.insert(7);
    list2.insert(4);
    list2.insert(7);
    list2.insert(7);
    list2.insert(5);
    list2.min_frequency_node();
	return 0;
}

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
//Include namespace system
using System;
// C# Program
// Count minimum frequency elements in a 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;
	}
}
//Frequency node counter
class NodeCounter
{
	public int data;
	public int frequency;
	public NodeCounter next;
	public NodeCounter(int data)
	{
		//Set initial node value
		this.data = data;
		//Set the initial node frequency
		this.frequency = 1;
		this.next = null;
	}
};
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructors
	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;
		}
		Console.Write("\n");
	}
	//Find node of given key in list
	public NodeCounter find_node(NodeCounter front, int key)
	{
		NodeCounter temp = front;
		while (temp != null)
		{
			if (temp.data == key)
			{
				//When key node exist
				return temp;
			}
			//visit to next node
			temp = temp.next;
		}
		//when key not exist
		return null;
	}
	//Count all nodes which containing of minimum frequency
	public void min_frequency_node()
	{
		if (this.head == null)
		{
			return;
		}
		//Display of linked list nodes
		this.display();
		//Define some auxiliary variables
		NodeCounter front = new NodeCounter(this.head.data);
		NodeCounter last = front;
		NodeCounter current = null;
		NodeCounter temp = null;
		//Resultant variables
		int counter = 1;
		//Get second node of linked list
		Node auxiliary = this.head.next;
		//Create auxiliary list which is contain data and element occurrence
		while (auxiliary != null)
		{
			//Find auxiliary node value in resultant list
			current = find_node(front, auxiliary.data);
			if (current != null)
			{
				//update node frequency
				current.frequency = current.frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				current = new NodeCounter(auxiliary.data);
				//add new node at end of auxiliary list
				last.next = current;
				last = current;
			}
			//visit to next node
			auxiliary = auxiliary.next;
		}
		//Start to first node
		temp = front;
		current = front;
		//Find minimum frequency
		while (temp != null)
		{
			if (current.data > temp.data)
			{
				//Get node of minimum frequency
				current = temp;
			}
			//visit to next node
			temp = temp.next;
		}
		if (current == null)
		{
			//When auxiliary list are empty
			Console.Write(" None ");
		}
		else
		{
			//Display the value of resultant minimum frequency
			Console.Write(" Result Minimum Frequency : " + current.frequency + " \n Nodes : [");
			//reset value
			counter = 0;
			last = null;
			//Count nodes which is contain minimum frequency
			while (front != null)
			{
				//Get current node
				temp = front;
				if (front.frequency == current.frequency)
				{
					//print node of which is contain minimum frequency
					Console.Write(" " + front.data);
					//count minimum frequency element 
					counter++;
				}
				//visit to next node
				front = front.next;
				//free node
				temp.next = null;
				temp = null;
			}
			//Display result
			Console.Write(" ]\n Result : " + counter + " \n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		//Create first linked list
		list1.insert(2);
		list1.insert(4);
		list1.insert(7);
		list1.insert(5);
		list1.insert(7);
		list1.insert(7);
		list1.insert(4);
		list1.insert(1);
		list1.insert(5);
		list1.insert(3);
		list1.insert(1);
		list1.insert(2);
		list1.insert(4);
		list1.min_frequency_node();
		//Create second linked list
		list2.insert(9);
		list2.insert(5);
		list2.insert(7);
		list2.insert(4);
		list2.insert(7);
		list2.insert(7);
		list2.insert(5);
		list2.min_frequency_node();
	}
}

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
<?php
// Php Program
// Count minimum frequency elements in a linked list

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

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->next = null;
	}
}
//Frequency node counter
class NodeCounter
{
	public $data;
	public $frequency;
	public $next;

	function __construct($data)
	{
		//Set initial node value
		$this->data = $data;
		//Set the initial node frequency
		$this->frequency = 1;
		$this->next = null;
	}
};
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructors
	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;
		}
		echo "\n";
	}
	//Find node of given key in list
	public	function find_node($front, $key)
	{
		$temp = $front;
		while ($temp != null)
		{
			if ($temp->data == $key)
			{
				//When key node exist
				return $temp;
			}
			//visit to next node
			$temp = $temp->next;
		}
		//when key not exist
		return null;
	}
	//Count all nodes which containing of minimum frequency
	public	function min_frequency_node()
	{
		if ($this->head == null)
		{
			return;
		}
		//Display of linked list nodes
		$this->display();
		//Define some auxiliary variables
		$front = new NodeCounter($this->head->data);
		$last = $front;
		$current = null;
		$temp = null;
		//Resultant variables
		$counter = 1;
		//Get second node of linked list
		$auxiliary = $this->head->next;
		//Create auxiliary list which is contain data and element occurrence
		while ($auxiliary != null)
		{
			//Find auxiliary node value in resultant list
			$current = $this->find_node($front, $auxiliary->data);
			if ($current != null)
			{
				//update node frequency
				$current->frequency = $current->frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				$current = new NodeCounter($auxiliary->data);
				//add new node at end of auxiliary list
				$last->next = $current;
				$last = $current;
			}
			//visit to next node
			$auxiliary = $auxiliary->next;
		}
		//Start to first node
		$temp = $front;
		$current = $front;
		//Find minimum frequency
		while ($temp != null)
		{
			if ($current->data > $temp->data)
			{
				//Get node of minimum frequency
				$current = $temp;
			}
			//visit to next node
			$temp = $temp->next;
		}
		if ($current == null)
		{
			//When auxiliary list are empty
			echo " None ";
		}
		else
		{
			//Display the value of resultant minimum frequency
			echo " Result Minimum Frequency : ". $current->frequency ." \n Nodes : [";
			//reset value
			$counter = 0;
			$last = null;
			//Count nodes which is contain minimum frequency
			while ($front != null)
			{
				//Get current node
				$temp = $front;
				if ($front->frequency == $current->frequency)
				{
					//print node of which is contain minimum frequency
					echo " ". $front->data;
					//count minimum frequency element 
					$counter++;
				}
				//visit to next node
				$front = $front->next;
				//free node
				$temp->next = null;
				$temp = null;
			}
			//Display result
			echo " ]\n Result : ". $counter ." \n";
		}
		echo "\n";
	}
}

function main()
{
	$list1 = new MyLinkedList();
	$list2 = new MyLinkedList();
	//Create first linked list
	$list1->insert(2);
	$list1->insert(4);
	$list1->insert(7);
	$list1->insert(5);
	$list1->insert(7);
	$list1->insert(7);
	$list1->insert(4);
	$list1->insert(1);
	$list1->insert(5);
	$list1->insert(3);
	$list1->insert(1);
	$list1->insert(2);
	$list1->insert(4);
	$list1->min_frequency_node();
	//Create second linked list
	$list2->insert(9);
	$list2->insert(5);
	$list2->insert(7);
	$list2->insert(4);
	$list2->insert(7);
	$list2->insert(7);
	$list2->insert(5);
	$list2->min_frequency_node();
}
main();

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
// Node Js Program
// Count minimum frequency elements in a linked list

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
//Frequency node counter
class NodeCounter
{
	constructor(data)
	{
		//Set initial node value
		this.data = data;
		//Set the initial node frequency
		this.frequency = 1;
		this.next = null;
	}
};
class MyLinkedList
{
	//Class constructors
	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;
		}
		process.stdout.write("\n");
	}
	//Find node of given key in list
	find_node(front, key)
	{
		var temp = front;
		while (temp != null)
		{
			if (temp.data == key)
			{
				//When key node exist
				return temp;
			}
			//visit to next node
			temp = temp.next;
		}
		//when key not exist
		return null;
	}
	//Count all nodes which containing of minimum frequency
	min_frequency_node()
	{
		if (this.head == null)
		{
			return;
		}
		//Display of linked list nodes
		this.display();
		//Define some auxiliary variables
		var front = new NodeCounter(this.head.data);
		var last = front;
		var current = null;
		var temp = null;
		//Resultant variables
		var counter = 1;
		//Get second node of linked list
		var auxiliary = this.head.next;
		//Create auxiliary list which is contain data and element occurrence
		while (auxiliary != null)
		{
			//Find auxiliary node value in resultant list
			current = this.find_node(front, auxiliary.data);
			if (current != null)
			{
				//update node frequency
				current.frequency = current.frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				current = new NodeCounter(auxiliary.data);
				//add new node at end of auxiliary list
				last.next = current;
				last = current;
			}
			//visit to next node
			auxiliary = auxiliary.next;
		}
		//Start to first node
		temp = front;
		current = front;
		//Find minimum frequency
		while (temp != null)
		{
			if (current.data > temp.data)
			{
				//Get node of minimum frequency
				current = temp;
			}
			//visit to next node
			temp = temp.next;
		}
		if (current == null)
		{
			//When auxiliary list are empty
			process.stdout.write(" None ");
		}
		else
		{
			//Display the value of resultant minimum frequency
			process.stdout.write(" Result Minimum Frequency : " + current.frequency + " \n Nodes : [");
			//reset value
			counter = 0;
			last = null;
			//Count nodes which is contain minimum frequency
			while (front != null)
			{
				//Get current node
				temp = front;
				if (front.frequency == current.frequency)
				{
					//print node of which is contain minimum frequency
					process.stdout.write(" " + front.data);
					//count minimum frequency element 
					counter++;
				}
				//visit to next node
				front = front.next;
				//free node
				temp.next = null;
				temp = null;
			}
			//Display result
			process.stdout.write(" ]\n Result : " + counter + " \n");
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var list1 = new MyLinkedList();
	var list2 = new MyLinkedList();
	//Create first linked list
	list1.insert(2);
	list1.insert(4);
	list1.insert(7);
	list1.insert(5);
	list1.insert(7);
	list1.insert(7);
	list1.insert(4);
	list1.insert(1);
	list1.insert(5);
	list1.insert(3);
	list1.insert(1);
	list1.insert(2);
	list1.insert(4);
	list1.min_frequency_node();
	//Create second linked list
	list2.insert(9);
	list2.insert(5);
	list2.insert(7);
	list2.insert(4);
	list2.insert(7);
	list2.insert(7);
	list2.insert(5);
	list2.min_frequency_node();
}
main();

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
#  Python 3 Program
#  Count minimum frequency elements in a linked list

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

# Frequency node counter
class NodeCounter :
	
	def __init__(self, data) :
		# Set initial node value
		self.data = data
		# Set the initial node frequency
		self.frequency = 1
		self.next = None
	

class MyLinkedList :
	
	# Class constructors
	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
		
		print("\n", end = "")
	
	# Find node of given key in list
	def find_node(self, front, key) :
		temp = front
		while (temp != None) :
			if (temp.data == key) :
				# When key node exist
				return temp
			
			# visit to next node
			temp = temp.next
		
		# when key not exist
		return None
	
	# Count all nodes which containing of minimum frequency
	def min_frequency_node(self) :
		if (self.head == None) :
			return
		
		# Display of linked list nodes
		self.display()
		# Define some auxiliary variables
		front = NodeCounter(self.head.data)
		last = front
		current = None
		temp = None
		# Resultant variables
		counter = 1
		# Get second node of linked list
		auxiliary = self.head.next
		# Create auxiliary list which is contain data and element occurrence
		while (auxiliary != None) :
			# Find auxiliary node value in resultant list
			current = self.find_node(front, auxiliary.data)
			if (current != None) :
				# update node frequency
				current.frequency = current.frequency + 1
			else :
				# create new node of auxiliary list
				current = NodeCounter(auxiliary.data)
				# add new node at end of auxiliary list
				last.next = current
				last = current
			
			# visit to next node
			auxiliary = auxiliary.next
		
		# Start to first node
		temp = front
		current = front
		# Find minimum frequency
		while (temp != None) :
			if (current.data > temp.data) :
				# Get node of minimum frequency
				current = temp
			
			# visit to next node
			temp = temp.next
		
		if (current == None) :
			# When auxiliary list are empty
			print(" None ", end = "")
		else :
			# Display the value of resultant minimum frequency
			print(" Result Minimum Frequency : ", current.frequency ," \n Nodes : [", end = "")
			# reset value
			counter = 0
			last = None
			# Count nodes which is contain minimum frequency
			while (front != None) :
				# Get current node
				temp = front
				if (front.frequency == current.frequency) :
					# print node of which is contain minimum frequency
					print(" ", front.data, end = "")
					# count minimum frequency element 
					counter += 1
				
				# visit to next node
				front = front.next
				# free node
				temp.next = None
				temp = None
			
			# Display result
			print(" ]\n Result : ", counter ," \n", end = "")
		
		print("\n", end = "")
	

def main() :
	list1 = MyLinkedList()
	list2 = MyLinkedList()
	# Create first linked list
	list1.insert(2)
	list1.insert(4)
	list1.insert(7)
	list1.insert(5)
	list1.insert(7)
	list1.insert(7)
	list1.insert(4)
	list1.insert(1)
	list1.insert(5)
	list1.insert(3)
	list1.insert(1)
	list1.insert(2)
	list1.insert(4)
	list1.min_frequency_node()
	# Create second linked list
	list2.insert(9)
	list2.insert(5)
	list2.insert(7)
	list2.insert(4)
	list2.insert(7)
	list2.insert(7)
	list2.insert(5)
	list2.min_frequency_node()

if __name__ == "__main__": main()

Output

 Linked List :    2   4   7   5   7   7   4   1   5   3   1   2   4
 Result Minimum Frequency :  2
 Nodes : [  2  5  1 ]
 Result :  3


 Linked List :    9   5   7   4   7   7   5
 Result Minimum Frequency :  1
 Nodes : [  9  4 ]
 Result :  2
#  Ruby Program
#  Count minimum frequency elements in a 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
# Frequency node counter
class NodeCounter 

	# Define the accessor and reader of class NodeCounter  
	attr_reader :data, :frequency, :next
	attr_accessor :data, :frequency, :next


	
	def initialize(data)
	
		# Set initial node value
		self.data = data
		# Set the initial node frequency
		self.frequency = 1
		self.next = nil
	end
end
class MyLinkedList 

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


	
	# Class constructors
	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
		print("\n")
	end
	# Find node of given key in list
	def find_node(front, key)
	
		temp = front
		while (temp != nil)
		
			if (temp.data == key)
			
				# When key node exist
				return temp
			end
			# visit to next node
			temp = temp.next
		end
		# when key not exist
		return nil
	end
	# Count all nodes which containing of minimum frequency
	def min_frequency_node()
	
		if (self.head == nil)
		
			return
		end
		# Display of linked list nodes
		self.display()
		# Define some auxiliary variables
		front = NodeCounter.new(self.head.data)
		last = front
		current = nil
		temp = nil
		# Resultant variables
		counter = 1
		# Get second node of linked list
		auxiliary = self.head.next
		# Create auxiliary list which is contain data and element occurrence
		while (auxiliary != nil)
		
			# Find auxiliary node value in resultant list
			current = self.find_node(front, auxiliary.data)
			if (current != nil)
			
				# update node frequency
				current.frequency = current.frequency + 1
			else
			
				# create new node of auxiliary list
				current = NodeCounter.new(auxiliary.data)
				# add new node at end of auxiliary list
				last.next = current
				last = current
			end
			# visit to next node
			auxiliary = auxiliary.next
		end
		# Start to first node
		temp = front
		current = front
		# Find minimum frequency
		while (temp != nil)
		
			if (current.data > temp.data)
			
				# Get node of minimum frequency
				current = temp
			end
			# visit to next node
			temp = temp.next
		end
		if (current == nil)
		
			# When auxiliary list are empty
			print(" None ")
		else
		
			# Display the value of resultant minimum frequency
			print(" Result Minimum Frequency : ", current.frequency ," \n Nodes : [")
			# reset value
			counter = 0
			last = nil
			# Count nodes which is contain minimum frequency
			while (front != nil)
			
				# Get current node
				temp = front
				if (front.frequency == current.frequency)
				
					# print node of which is contain minimum frequency
					print(" ", front.data)
					# count minimum frequency element 
					counter += 1
				end
				# visit to next node
				front = front.next
				# free node
				temp.next = nil
				temp = nil
			end
			# Display result
			print(" ]\n Result : ", counter ," \n")
		end
		print("\n")
	end
end
def main()

	list1 = MyLinkedList.new()
	list2 = MyLinkedList.new()
	# Create first linked list
	list1.insert(2)
	list1.insert(4)
	list1.insert(7)
	list1.insert(5)
	list1.insert(7)
	list1.insert(7)
	list1.insert(4)
	list1.insert(1)
	list1.insert(5)
	list1.insert(3)
	list1.insert(1)
	list1.insert(2)
	list1.insert(4)
	list1.min_frequency_node()
	# Create second linked list
	list2.insert(9)
	list2.insert(5)
	list2.insert(7)
	list2.insert(4)
	list2.insert(7)
	list2.insert(7)
	list2.insert(5)
	list2.min_frequency_node()
end
main()

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2 
 Nodes : [ 2 5 1 ]
 Result : 3 


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1 
 Nodes : [ 9 4 ]
 Result : 2 

// Scala Program
// Count minimum frequency elements in a linked list

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
//Frequency node counter
class NodeCounter(var data: Int,
		var frequency: Int,
		var next: NodeCounter)
{
	def this(data: Int)
	{
		this(data, 1, null);
	}
}
class MyLinkedList(var head: Node,
	var tail: Node)
{
	//Class constructors
	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;
		}
		print("\n");
	}
	//Find node of given key in list
	def find_node(front: NodeCounter, key: Int): NodeCounter = {
		var temp: NodeCounter = front;
		while (temp != null)
		{
			if (temp.data == key)
			{
				//When key node exist
				return temp;
			}
			//visit to next node
			temp = temp.next;
		}
		//when key not exist
		return null;
	}
	//Count all nodes which containing of minimum frequency
	def min_frequency_node(): Unit = {
		if (this.head == null)
		{
			return;
		}
		//Display of linked list nodes
		this.display();
		//Define some auxiliary variables
		var front: NodeCounter = new NodeCounter(this.head.data);
		var last: NodeCounter = front;
		var current: NodeCounter = null;
		var temp: NodeCounter = null;
		//Resultant variables
		var counter: Int = 1;
		//Get second node of linked list
		var auxiliary: Node = this.head.next;
		//Create auxiliary list which is contain data and element occurrence
		while (auxiliary != null)
		{
			//Find auxiliary node value in resultant list
			current = find_node(front, auxiliary.data);
			if (current != null)
			{
				//update node frequency
				current.frequency = current.frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				current = new NodeCounter(auxiliary.data);
				//add new node at end of auxiliary list
				last.next = current;
				last = current;
			}
			//visit to next node
			auxiliary = auxiliary.next;
		}
		//Start to first node
		temp = front;
		current = front;
		//Find minimum frequency
		while (temp != null)
		{
			if (current.data > temp.data)
			{
				//Get node of minimum frequency
				current = temp;
			}
			//visit to next node
			temp = temp.next;
		}
		if (current == null)
		{
			//When auxiliary list are empty
			print(" None ");
		}
		else
		{
			//Display the value of resultant minimum frequency
			print(" Result Minimum Frequency : " + current.frequency + " \n Nodes : [");
			//reset value
			counter = 0;
			last = null;
			//Count nodes which is contain minimum frequency
			while (front != null)
			{
				//Get current node
				temp = front;
				if (front.frequency == current.frequency)
				{
					//print node of which is contain minimum frequency
					print(" " + front.data);
					//count minimum frequency element 
					counter += 1;
				}
				//visit to next node
				front = front.next;
				//free node
				temp.next = null;
				temp = null;
			}
			//Display result
			print(" ]\n Result : " + counter + " \n");
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var list1: MyLinkedList = new MyLinkedList();
		var list2: MyLinkedList = new MyLinkedList();
		//Create first linked list
		list1.insert(2);
		list1.insert(4);
		list1.insert(7);
		list1.insert(5);
		list1.insert(7);
		list1.insert(7);
		list1.insert(4);
		list1.insert(1);
		list1.insert(5);
		list1.insert(3);
		list1.insert(1);
		list1.insert(2);
		list1.insert(4);
		list1.min_frequency_node();
		//Create second linked list
		list2.insert(9);
		list2.insert(5);
		list2.insert(7);
		list2.insert(4);
		list2.insert(7);
		list2.insert(7);
		list2.insert(5);
		list2.min_frequency_node();
	}
}

Output

 Linked List :   2  4  7  5  7  7  4  1  5  3  1  2  4
 Result Minimum Frequency : 2
 Nodes : [ 2 5 1 ]
 Result : 3


 Linked List :   9  5  7  4  7  7  5
 Result Minimum Frequency : 1
 Nodes : [ 9 4 ]
 Result : 2
// Swift Program
// Count minimum frequency elements in a 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;
	}
}
//Frequency node counter
class NodeCounter
{
	var data: Int;
	var frequency: Int;
	var next: NodeCounter? ;
	init(_ data: Int)
	{
		//Set initial node value
		self.data = data;
		//Set the initial node frequency
		self.frequency = 1;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructors
	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;
		}
		print("\n", terminator: "");
	}
	//Find node of given key in list
	func find_node(_ front: NodeCounter? , _ key : Int) -> NodeCounter?
	{
		var temp: NodeCounter? = front;
		while (temp != nil)
		{
			if (temp!.data == key)
			{
				//When key node exist
				return temp;
			}
			//visit to next node
			temp = temp!.next;
		}
		//when key not exist
		return nil;
	}
	//Count all nodes which containing of minimum frequency
	func min_frequency_node()
	{
		if (self.head == nil)
		{
			return;
		}
		//Display of linked list nodes
		self.display();
		//Define some auxiliary variables
		var front: NodeCounter? = NodeCounter(self.head!.data);
		var last: NodeCounter? = front;
		var current: NodeCounter? = nil;
		var temp: NodeCounter? = nil;
		//Resultant variables
		var counter: Int = 1;
		//Get second node of linked list
		var auxiliary: Node? = self.head!.next;
		//Create auxiliary list which is contain data and element occurrence
		while (auxiliary != nil)
		{
			//Find auxiliary node value in resultant list
			current = self.find_node(front, auxiliary!.data);
			if (current != nil)
			{
				//update node frequency
				current!.frequency = current!.frequency + 1;
			}
			else
			{
				//create new node of auxiliary list
				current = NodeCounter(auxiliary!.data);
				//add new node at end of auxiliary list
				last!.next = current;
				last = current;
			}
			//visit to next node
			auxiliary = auxiliary!.next;
		}
		//Start to first node
		temp = front;
		current = front;
		//Find minimum frequency
		while (temp != nil)
		{
			if (current!.data > temp!.data)
			{
				//Get node of minimum frequency
				current = temp;
			}
			//visit to next node
			temp = temp!.next;
		}
		if (current == nil)
		{
			//When auxiliary list are empty
			print(" None ", terminator: "");
		}
		else
		{
			//Display the value of resultant minimum frequency
			print(" Result Minimum Frequency : ", current!.frequency ," \n Nodes : [", terminator: "");
			//reset value
			counter = 0;
			last = nil;
			//Count nodes which is contain minimum frequency
			while (front != nil)
			{
				//Get current node
				temp = front;
				if (front!.frequency == current!.frequency)
				{
					//print node of which is contain minimum frequency
					print(" ", front!.data, terminator: "");
					//count minimum frequency element 
					counter += 1;
				}
				//visit to next node
				front = front!.next;
				//free node
				temp!.next = nil;
				temp = nil;
			}
			//Display result
			print(" ]\n Result : ", counter ," \n", terminator: "");
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let list1: MyLinkedList = MyLinkedList();
	let list2: MyLinkedList = MyLinkedList();
	//Create first linked list
	list1.insert(2);
	list1.insert(4);
	list1.insert(7);
	list1.insert(5);
	list1.insert(7);
	list1.insert(7);
	list1.insert(4);
	list1.insert(1);
	list1.insert(5);
	list1.insert(3);
	list1.insert(1);
	list1.insert(2);
	list1.insert(4);
	list1.min_frequency_node();
	//Create second linked list
	list2.insert(9);
	list2.insert(5);
	list2.insert(7);
	list2.insert(4);
	list2.insert(7);
	list2.insert(7);
	list2.insert(5);
	list2.min_frequency_node();
}
main();

Output

 Linked List :    2   4   7   5   7   7   4   1   5   3   1   2   4
 Result Minimum Frequency :  2
 Nodes : [  2  5  1 ]
 Result :  3


 Linked List :    9   5   7   4   7   7   5
 Result Minimum Frequency :  1
 Nodes : [  9  4 ]
 Result :  2


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