Posted on by Kalkicode

# Count all prime nodes of a linked list

Here given code implementation process.

``````// C Program
// Count all prime nodes of a linked list
#include <stdio.h>
//For malloc function
#include <stdlib.h>

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;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
//visit to next node
temp = temp->next;
}
printf("\n");
}
//Check that whether given number is prime or not
int is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return 1;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return 0;
}
int i = 11;
while ((i *i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return 0;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return 0;
}
i = i + 6;
}
return 1;
}
//Count the number of linked list nodes which is contains  prime values
{
// This are used to count prime nodes
int counter = 0;
//Get first node
printf(" Prime Nodes : [");
while (temp != NULL)
{
if (is_prime(temp->data) == 1)
{
printf("  %d", temp->data);
counter++;
}
//visit to next node
temp = temp->next;
}
printf("  ]\n");
//Display the calculated result
printf(" Total Prime :  %d \n", counter);
}
int main()
{
return 0;
}``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes : [  2  5  11  7  31  ]
Total Prime :  5``````
``````// Java Program
// Count all prime nodes of a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
System.out.print("  " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print("\n");
}
//Check that whether given number is prime or not
public boolean is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
public void count_prime()
{
this.display();
// This are used to count prime nodes
int counter = 0;
//Get first node
System.out.print(" Prime Nodes :   [");
while (temp != null)
{
if (is_prime(temp.data) == true)
{
System.out.print(" " + temp.data);
counter++;
}
//visit to next node
temp = temp.next;
}
System.out.print(" ]\n");
//Display the calculated result
System.out.print(" Total Prime :   " + counter + " \n");
}
public static void main(String[] args)
{
obj.insert(2);
obj.insert(4);
obj.insert(5);
obj.insert(11);
obj.insert(12);
obj.insert(7);
obj.insert(124);
obj.insert(31);
obj.count_prime();
}
}``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Count all prime nodes of a linked list

class Node
{
public: int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
//display node value
cout << "  " << temp->data;
//visit to next node
temp = temp->next;
}
cout << "\n";
}
//Check that whether given number is prime or not
bool is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
void count_prime()
{
this->display();
// This are used to count prime nodes
int counter = 0;
//Get first node
cout << " Prime Nodes :   [";
while (temp != NULL)
{
if (this->is_prime(temp->data) == true)
{
cout << " " << temp->data;
counter++;
}
//visit to next node
temp = temp->next;
}
cout << " ]\n";
//Display the calculated result
cout << " Total Prime :   " << counter << " \n";
}
};
int main()
{
obj.insert(2);
obj.insert(4);
obj.insert(5);
obj.insert(11);
obj.insert(12);
obj.insert(7);
obj.insert(124);
obj.insert(31);
obj.count_prime();
return 0;
}``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5``````
``````//Include namespace system
using System;
// C# Program
// Count all prime nodes of a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
Console.Write("  " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write("\n");
}
//Check that whether given number is prime or not
public Boolean is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
public void count_prime()
{
this.display();
// This are used to count prime nodes
int counter = 0;
//Get first node
Console.Write(" Prime Nodes :   [");
while (temp != null)
{
if (is_prime(temp.data) == true)
{
Console.Write(" " + temp.data);
counter++;
}
//visit to next node
temp = temp.next;
}
Console.Write(" ]\n");
//Display the calculated result
Console.Write(" Total Prime :   " + counter + " \n");
}
public static void Main(String[] args)
{
obj.insert(2);
obj.insert(4);
obj.insert(5);
obj.insert(11);
obj.insert(12);
obj.insert(7);
obj.insert(124);
obj.insert(31);
obj.count_prime();
}
}``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5``````
``````<?php
// Php Program
// Count all prime nodes of a linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
//display node value
echo "  ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo "\n";
}
//Check that whether given number is prime or not
public	function is_prime(\$num)
{
if (\$num == 2 || \$num == 3 || \$num == 5)
{
return true;
}
if (\$num <= 1 || (\$num % 2 == 0) || (\$num % 3 == 0) || (\$num % 5 == 0))
{
return false;
}
\$i = 11;
while ((\$i * \$i) <= \$num)
{
if (\$num % \$i == 0)
{
//When number is divisible of current i value
return false;
}
else if (\$num % (\$i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
\$i = \$i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
public	function count_prime()
{
\$this->display();
// This are used to count prime nodes
\$counter = 0;
//Get first node
echo " Prime Nodes :   [";
while (\$temp != null)
{
if (\$this->is_prime(\$temp->data) == true)
{
echo " ". \$temp->data;
\$counter++;
}
//visit to next node
\$temp = \$temp->next;
}
echo " ]\n";
//Display the calculated result
echo " Total Prime :   ". \$counter ." \n";
}
}

function main()
{
\$obj->insert(2);
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(11);
\$obj->insert(12);
\$obj->insert(7);
\$obj->insert(124);
\$obj->insert(31);
\$obj->count_prime();
}
main();``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5``````
``````// Node Js Program
// Count all prime nodes of a linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
//display node value
process.stdout.write("  " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write("\n");
}
//Check that whether given number is prime or not
is_prime(num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
count_prime()
{
this.display();
// This are used to count prime nodes
var counter = 0;
//Get first node
process.stdout.write(" Prime Nodes :   [");
while (temp != null)
{
if (this.is_prime(temp.data) == true)
{
process.stdout.write(" " + temp.data);
counter++;
}
//visit to next node
temp = temp.next;
}
process.stdout.write(" ]\n");
//Display the calculated result
process.stdout.write(" Total Prime :   " + counter + " \n");
}
}

function main()
{
obj.insert(2);
obj.insert(4);
obj.insert(5);
obj.insert(11);
obj.insert(12);
obj.insert(7);
obj.insert(124);
obj.insert(31);
obj.count_prime();
}
main();``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5``````
``````#  Python 3 Program
#  Count all prime nodes of a linked list

class Node :

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

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List : ", end = "")
while (temp != None) :
# display node value
print("  ", temp.data, end = "")
# visit to next node
temp = temp.next

print("\n", end = "")

# Check that whether given number is prime or not
def is_prime(self, num) :
if (num == 2 or num == 3 or num == 5) :
return True

if (num <= 1 or(num % 2 == 0) or(num % 3 == 0) or(num % 5 == 0)) :
return False

i = 11
while ((i * i) <= num) :
if (num % i == 0) :
# When number is divisible of current i value
return False

elif(num % (i + 2) == 0) :
# When number is divisible of current i + 2 value
return False

i = i + 6

return True

# Count the number of linked list nodes which is contains  prime values
def count_prime(self) :
self.display()
#  This are used to count prime nodes
counter = 0
# Get first node
print(" Prime Nodes :   [", end = "")
while (temp != None) :
if (self.is_prime(temp.data) == True) :
print(" ", temp.data, end = "")
counter += 1

# visit to next node
temp = temp.next

print(" ]\n", end = "")
# Display the calculated result
print(" Total Prime :   ", counter ," \n", end = "")

def main() :
obj.insert(2)
obj.insert(4)
obj.insert(5)
obj.insert(11)
obj.insert(12)
obj.insert(7)
obj.insert(124)
obj.insert(31)
obj.count_prime()

if __name__ == "__main__": main()``````

#### Output

`````` Linked List :    2   4   5   11   12   7   124   31
Prime Nodes :   [  2  5  11  7  31 ]
Total Prime :    5``````
``````#  Ruby Program
#  Count all prime nodes of a linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

# display node value
print("  ", temp.data)
# visit to next node
temp = temp.next
end
print("\n")
end
# Check that whether given number is prime or not
def is_prime(num)

if (num == 2 || num == 3 || num == 5)

return true
end
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))

return false
end
i = 11
while ((i * i) <= num)

if (num % i == 0)

# When number is divisible of current i value
return false
elsif(num % (i + 2) == 0)

# When number is divisible of current i + 2 value
return false
end
i = i + 6
end
return true
end
# Count the number of linked list nodes which is contains  prime values
def count_prime()

self.display()
#  This are used to count prime nodes
counter = 0
# Get first node
print(" Prime Nodes :   [")
while (temp != nil)

if (self.is_prime(temp.data) == true)

print(" ", temp.data)
counter += 1
end
# visit to next node
temp = temp.next
end
print(" ]\n")
# Display the calculated result
print(" Total Prime :   ", counter ," \n")
end
end
def main()

obj.insert(2)
obj.insert(4)
obj.insert(5)
obj.insert(11)
obj.insert(12)
obj.insert(7)
obj.insert(124)
obj.insert(31)
obj.count_prime()
end
main()``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5
``````
``````// Scala Program
// Count all prime nodes of a linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
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);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
//display node value
print("  " + temp.data);
//visit to next node
temp = temp.next;
}
print("\n");
}
//Check that whether given number is prime or not
def is_prime(num: Int): Boolean = {
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
def count_prime(): Unit = {
this.display();
// This are used to count prime nodes
var counter: Int = 0;
//Get first node
print(" Prime Nodes :   [");
while (temp != null)
{
if (is_prime(temp.data) == true)
{
print(" " + temp.data);
counter += 1;
}
//visit to next node
temp = temp.next;
}
print(" ]\n");
//Display the calculated result
print(" Total Prime :   " + counter + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(2);
obj.insert(4);
obj.insert(5);
obj.insert(11);
obj.insert(12);
obj.insert(7);
obj.insert(124);
obj.insert(31);
obj.count_prime();
}
}``````

#### Output

`````` Linked List :   2  4  5  11  12  7  124  31
Prime Nodes :   [ 2 5 11 7 31 ]
Total Prime :   5``````
``````// Swift Program
// Count all prime nodes of a linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
//display node value
print("  ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print("\n", terminator: "");
}
//Check that whether given number is prime or not
func is_prime(_ num: Int) -> Bool
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Count the number of linked list nodes which is contains  prime values
func count_prime()
{
self.display();
// This are used to count prime nodes
var counter: Int = 0;
//Get first node
print(" Prime Nodes :   [", terminator: "");
while (temp != nil)
{
if (self.is_prime(temp!.data) == true)
{
print(" ", temp!.data, terminator: "");
counter += 1;
}
//visit to next node
temp = temp!.next;
}
print(" ]\n", terminator: "");
//Display the calculated result
print(" Total Prime :   ", counter ," \n", terminator: "");
}
}
func main()
{
obj.insert(2);
obj.insert(4);
obj.insert(5);
obj.insert(11);
obj.insert(12);
obj.insert(7);
obj.insert(124);
obj.insert(31);
obj.count_prime();
}
main();``````

#### Output

`````` Linked List :    2   4   5   11   12   7   124   31
Prime Nodes :   [  2  5  11  7  31 ]
Total Prime :    5``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post