Find probability of a key in linked list

Here given code implementation process.

// C Program
// Find probability of a key 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 : ");

    while (temp != NULL)
    {   
       
        printf("  %d", temp->data); 
        
       
        temp = temp->next;
    }
    printf("\n");
   
}


//Find probability of a key in linked list
void node_probability(struct Node*head,int key)
{
   
    //Define resultant variable
    //This is used to count key nodes
    int counter = 0;

    //used to store number of nodes
    int size  = 0;
    
    //Get first node of linked list
    struct Node*temp = head;

    //iterate linked list node
    while( temp != NULL)
    {
        if(key == temp->data)
        {
            //When key node exist
            counter++;  
        }
        //count number of nodes
        size++;
        //Visit to next node
        temp = temp->next;
    }
    if(counter!=0 && size!=0)
    {
        //Display of calculated result
        printf(" Probability of key %d are : %lf\n",key,(counter/(double)size));
    }
    else
    {
         printf(" Probability of key %d are : %d\n",key,0);
    }
    
}

int main()
{

    struct Node *head = NULL;
   
    //Create linked list
    insert(&head,1);
    insert(&head,6);
    insert(&head,111);
    insert(&head,3);
    insert(&head,1);
    insert(&head,9);
    insert(&head,1);
    insert(&head,3);
    insert(&head,11);
    insert(&head,1);
    insert(&head,21);
    
    //Display of linked list nodes
    display(head);

    //Test case
    node_probability(head,1);
    node_probability(head,3);
    node_probability(head,13);
    node_probability(head,9);

    return 0;
}

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21
 Probability of key 1 are : 0.363636
 Probability of key 3 are : 0.181818
 Probability of key 13 are : 0
 Probability of key 9 are : 0.090909
// Java Program
// Find probability of a key 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;
	}
}
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 : ");
		while (temp != null)
		{
			System.out.print("  " + temp.data);
			temp = temp.next;
		}
	}
	//Find probability of a key in linked list
	public void node_probability(int key)
	{
		//Define resultant variable
		//This is used to count key nodes
		int counter = 0;
		//used to store number of nodes
		int size = 0;
		//Get first node of linked list
		Node temp = head;
		//iterate linked list node
		while (temp != null)
		{
			if (key == temp.data)
			{
				//When key node exist
				counter++;
			}
			//count number of nodes
			size++;
			//Visit to next node
			temp = temp.next;
		}
		if (counter != 0 && size != 0)
		{
			//Display of calculated result
			System.out.print(" Probability of key " + key + " are : " + (counter / (double) size) + "\n");
		}
		else
		{
			System.out.print(" Probability of key " + key + " are : 0\n");
		}
	}
	public static void main(String[] args)
	{
		MyLinkedList obj = new MyLinkedList();
		//Add node in linked list
		obj.insert(1);
		obj.insert(6);
		obj.insert(111);
		obj.insert(3);
		obj.insert(1);
		obj.insert(9);
		obj.insert(1);
		obj.insert(3);
		obj.insert(11);
		obj.insert(1);
		obj.insert(21);
		//Display of linked list nodes
		obj.display();
		//Test case
		obj.node_probability(1);
		obj.node_probability(3);
		obj.node_probability(13);
		obj.node_probability(9);
	}
}

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.36363636363636365
 Probability of key 3 are : 0.18181818181818182
 Probability of key 13 are : 0
 Probability of key 9 are : 0.09090909090909091
//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Find probability of a key 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 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 : ";
		while (temp != NULL)
		{
			cout << "  " << temp->data;
			temp = temp->next;
		}
	}
	//Find probability of a key in linked list
	void node_probability(int key)
	{
		//Define resultant variable
		//This is used to count key nodes
		int counter = 0;
		//used to store number of nodes
		int size = 0;
		//Get first node of linked list
		Node * temp = this->head;
		//iterate linked list node
		while (temp != NULL)
		{
			if (key == temp->data)
			{
				//When key node exist
				counter++;
			}
			//count number of nodes
			size++;
			//Visit to next node
			temp = temp->next;
		}
		if (counter != 0 && size != 0)
		{
			//Display of calculated result
			cout << " Probability of key " << key << " are : " << (counter / (double) size) << "\n";
		}
		else
		{
			cout << " Probability of key " << key << " are : 0\n";
		}
	}
};
int main()
{
	MyLinkedList obj = MyLinkedList();
	//Add node in linked list
	obj.insert(1);
	obj.insert(6);
	obj.insert(111);
	obj.insert(3);
	obj.insert(1);
	obj.insert(9);
	obj.insert(1);
	obj.insert(3);
	obj.insert(11);
	obj.insert(1);
	obj.insert(21);
	//Display of linked list nodes
	obj.display();
	//Test case
	obj.node_probability(1);
	obj.node_probability(3);
	obj.node_probability(13);
	obj.node_probability(9);
	return 0;
}

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.363636
 Probability of key 3 are : 0.181818
 Probability of key 13 are : 0
 Probability of key 9 are : 0.0909091
//Include namespace system
using System;

// C# Program
// Find probability of a key 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;
	}
}
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 : ");
		while (temp != null)
		{
			Console.Write("  " + temp.data);
			temp = temp.next;
		}
	}
	//Find probability of a key in linked list
	public void node_probability(int key)
	{
		//Define resultant variable
		//This is used to count key nodes
		int counter = 0;
		//used to store number of nodes
		int size = 0;
		//Get first node of linked list
		Node temp = head;
		//iterate linked list node
		while (temp != null)
		{
			if (key == temp.data)
			{
				//When key node exist
				counter++;
			}
			//count number of nodes
			size++;
			//Visit to next node
			temp = temp.next;
		}
		if (counter != 0 && size != 0)
		{
			//Display of calculated result
			Console.Write(" Probability of key " + key + " are : " + (counter / (double) size) + "\n");
		}
		else
		{
			Console.Write(" Probability of key " + key + " are : 0\n");
		}
	}
	public static void Main(String[] args)
	{
		MyLinkedList obj = new MyLinkedList();
		//Add node in linked list
		obj.insert(1);
		obj.insert(6);
		obj.insert(111);
		obj.insert(3);
		obj.insert(1);
		obj.insert(9);
		obj.insert(1);
		obj.insert(3);
		obj.insert(11);
		obj.insert(1);
		obj.insert(21);
		//Display of linked list nodes
		obj.display();
		//Test case
		obj.node_probability(1);
		obj.node_probability(3);
		obj.node_probability(13);
		obj.node_probability(9);
	}
}

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.363636363636364
 Probability of key 3 are : 0.181818181818182
 Probability of key 13 are : 0
 Probability of key 9 are : 0.0909090909090909
<?php
// Php Program
// Find probability of a key 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 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 : ";
		while ($temp != null)
		{
			echo "  ". $temp->data;
			$temp = $temp->next;
		}
	}
	//Find probability of a key in linked list
	public	function node_probability($key)
	{
		//Define resultant variable
		//This is used to count key nodes
		$counter = 0;
		//used to store number of nodes
		$size = 0;
		//Get first node of linked list
		$temp = $this->head;
		//iterate linked list node
		while ($temp != null)
		{
			if ($key == $temp->data)
			{
				//When key node exist
				$counter++;
			}
			//count number of nodes
			$size++;
			//Visit to next node
			$temp = $temp->next;
		}
		if ($counter != 0 && $size != 0)
		{
			//Display of calculated result
			echo " Probability of key ". $key ." are : ". (($counter / ($size))) ."\n";
		}
		else
		{
			echo " Probability of key ". $key ." are : 0\n";
		}
	}
}

function main()
{
	$obj = new MyLinkedList();
	//Add node in linked list
	$obj->insert(1);
	$obj->insert(6);
	$obj->insert(111);
	$obj->insert(3);
	$obj->insert(1);
	$obj->insert(9);
	$obj->insert(1);
	$obj->insert(3);
	$obj->insert(11);
	$obj->insert(1);
	$obj->insert(21);
	//Display of linked list nodes
	$obj->display();
	//Test case
	$obj->node_probability(1);
	$obj->node_probability(3);
	$obj->node_probability(13);
	$obj->node_probability(9);
}
main();

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.36363636363636
 Probability of key 3 are : 0.18181818181818
 Probability of key 13 are : 0
 Probability of key 9 are : 0.090909090909091
// Node Js Program
// Find probability of a key in linked list

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		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 : ");
		while (temp != null)
		{
			process.stdout.write("  " + temp.data);
			temp = temp.next;
		}
	}
	//Find probability of a key in linked list
	node_probability(key)
	{
		//Define resultant variable
		//This is used to count key nodes
		var counter = 0;
		//used to store number of nodes
		var size = 0;
		//Get first node of linked list
		var temp = this.head;
		//iterate linked list node
		while (temp != null)
		{
			if (key == temp.data)
			{
				//When key node exist
				counter++;
			}
			//count number of nodes
			size++;
			//Visit to next node
			temp = temp.next;
		}
		if (counter != 0 && size != 0)
		{
			//Display of calculated result
			process.stdout.write(" Probability of key " + key + " are : " + ((counter / (size))) + "\n");
		}
		else
		{
			process.stdout.write(" Probability of key " + key + " are : 0\n");
		}
	}
}

function main()
{
	var obj = new MyLinkedList();
	//Add node in linked list
	obj.insert(1);
	obj.insert(6);
	obj.insert(111);
	obj.insert(3);
	obj.insert(1);
	obj.insert(9);
	obj.insert(1);
	obj.insert(3);
	obj.insert(11);
	obj.insert(1);
	obj.insert(21);
	//Display of linked list nodes
	obj.display();
	//Test case
	obj.node_probability(1);
	obj.node_probability(3);
	obj.node_probability(13);
	obj.node_probability(9);
}
main();

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.36363636363636365
 Probability of key 3 are : 0.18181818181818182
 Probability of key 13 are : 0
 Probability of key 9 are : 0.09090909090909091
#  Python 3 Program
#  Find probability of a key in linked list

# Node of LinkedList
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		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 = "")
		while (temp != None) :
			print("  ", temp.data, end = "")
			temp = temp.next
		
	
	# Find probability of a key in linked list
	def node_probability(self, key) :
		# Define resultant variable
		# This is used to count key nodes
		counter = 0
		# used to store number of nodes
		size = 0
		# Get first node of linked list
		temp = self.head
		# iterate linked list node
		while (temp != None) :
			if (key == temp.data) :
				# When key node exist
				counter += 1
			
			# count number of nodes
			size += 1
			# Visit to next node
			temp = temp.next
		
		if (counter != 0 and size != 0) :
			# Display of calculated result
			print(" Probability of key ", key ," are : ", ((counter / (size))) ,"\n", end = "")
		else :
			print(" Probability of key ", key ," are : 0\n", end = "")
		
	

def main() :
	obj = MyLinkedList()
	# Add node in linked list
	obj.insert(1)
	obj.insert(6)
	obj.insert(111)
	obj.insert(3)
	obj.insert(1)
	obj.insert(9)
	obj.insert(1)
	obj.insert(3)
	obj.insert(11)
	obj.insert(1)
	obj.insert(21)
	# Display of linked list nodes
	obj.display()
	# Test case
	obj.node_probability(1)
	obj.node_probability(3)
	obj.node_probability(13)
	obj.node_probability(9)

if __name__ == "__main__": main()

Output

 Linked List :    1   6   111   3   1   9   1   3   11   1   21 Probability of key  1  are :  0.36363636363636365
 Probability of key  3  are :  0.18181818181818182
 Probability of key  13  are : 0
 Probability of key  9  are :  0.09090909090909091
#  Ruby Program
#  Find probability of a key 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 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 : ")
		while (temp != nil)
		
			print("  ", temp.data)
			temp = temp.next
		end
	end
	# Find probability of a key in linked list
	def node_probability(key)
	
		# Define resultant variable
		# This is used to count key nodes
		counter = 0
		# used to store number of nodes
		size = 0
		# Get first node of linked list
		temp = @head
		# iterate linked list node
		while (temp != nil)
		
			if (key == temp.data)
			
				# When key node exist
				counter += 1
			end
			# count number of nodes
			size += 1
			# Visit to next node
			temp = temp.next
		end
		if (counter != 0 && size != 0)
		
			# Display of calculated result
			print(" Probability of key ", key ," are : ", (counter / (size).to_f) ,"\n")
		else
		
			print(" Probability of key ", key ," are : 0\n")
		end
	end
end
def main()

	obj = MyLinkedList.new()
	# Add node in linked list
	obj.insert(1)
	obj.insert(6)
	obj.insert(111)
	obj.insert(3)
	obj.insert(1)
	obj.insert(9)
	obj.insert(1)
	obj.insert(3)
	obj.insert(11)
	obj.insert(1)
	obj.insert(21)
	# Display of linked list nodes
	obj.display()
	# Test case
	obj.node_probability(1)
	obj.node_probability(3)
	obj.node_probability(13)
	obj.node_probability(9)
end
main()

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.36363636363636365
 Probability of key 3 are : 0.18181818181818182
 Probability of key 13 are : 0
 Probability of key 9 are : 0.09090909090909091
// Scala Program
// Find probability of a key 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 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 : ");
		while (temp != null)
		{
			print("  " + temp.data);
			temp = temp.next;
		}
	}
	//Find probability of a key in linked list
	def node_probability(key: Int): Unit = {
		//Define resultant variable
		//This is used to count key nodes
		var counter: Int = 0;
		//used to store number of nodes
		var size: Int = 0;
		//Get first node of linked list
		var temp: Node = head;
		//iterate linked list node
		while (temp != null)
		{
			if (key == temp.data)
			{
				//When key node exist
				counter += 1;
			}
			//count number of nodes
			size += 1;
			//Visit to next node
			temp = temp.next;
		}
		if (counter != 0 && size != 0)
		{
			//Display of calculated result
			print(" Probability of key " + key + " are : " + ((counter / (size).toDouble)) + "\n");
		}
		else
		{
			print(" Probability of key " + key + " are : 0\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyLinkedList = new MyLinkedList();
		//Add node in linked list
		obj.insert(1);
		obj.insert(6);
		obj.insert(111);
		obj.insert(3);
		obj.insert(1);
		obj.insert(9);
		obj.insert(1);
		obj.insert(3);
		obj.insert(11);
		obj.insert(1);
		obj.insert(21);
		//Display of linked list nodes
		obj.display();
		//Test case
		obj.node_probability(1);
		obj.node_probability(3);
		obj.node_probability(13);
		obj.node_probability(9);
	}
}

Output

 Linked List :   1  6  111  3  1  9  1  3  11  1  21 Probability of key 1 are : 0.36363636363636365
 Probability of key 3 are : 0.18181818181818182
 Probability of key 13 are : 0
 Probability of key 9 are : 0.09090909090909091
// Swift Program
// Find probability of a key 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 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: "");
		while (temp != nil)
		{
			print("  ", temp!.data, terminator: "");
			temp = temp!.next;
		}
	}
	//Find probability of a key in linked list
	func node_probability(_ key: Int)
	{
		//Define resultant variable
		//This is used to count key nodes
		var counter: Int = 0;
		//used to store number of nodes
		var size: Int = 0;
		//Get first node of linked list
		var temp: Node? = self.head;
		//iterate linked list node
		while (temp != nil)
		{
			if (key == temp!.data)
			{
				//When key node exist
				counter += 1;
			}
			//count number of nodes
			size += 1;
			//Visit to next node
			temp = temp!.next;
		}
		if (counter != 0 && size != 0)
		{
			//Display of calculated result
			print(" Probability of key ", key ," are : ", (Double(counter) / Double(size)) ,"\n", terminator: "");
		}
		else
		{
			print(" Probability of key ", key ," are : 0\n", terminator: "");
		}
	}
}
func main()
{
	let obj: MyLinkedList = MyLinkedList();
	//Add node in linked list
	obj.insert(1);
	obj.insert(6);
	obj.insert(111);
	obj.insert(3);
	obj.insert(1);
	obj.insert(9);
	obj.insert(1);
	obj.insert(3);
	obj.insert(11);
	obj.insert(1);
	obj.insert(21);
	//Display of linked list nodes
	obj.display();
	//Test case
	obj.node_probability(1);
	obj.node_probability(3);
	obj.node_probability(13);
	obj.node_probability(9);
}
main();

Output

 Linked List :    1   6   111   3   1   9   1   3   11   1   21 Probability of key  1  are :  0.363636363636364
 Probability of key  3  are :  0.181818181818182
 Probability of key  13  are : 0
 Probability of key  9  are :  0.0909090909090909


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