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>
//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;
}
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
void count_prime(struct Node *head)
{
display(head);
// This are used to count prime nodes
int counter = 0;
//Get first node
struct Node *temp = head;
printf(" Prime Nodes : [");
//iterating linked list elements
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()
{
struct Node *head = NULL;
//Create linked list
insert( &head, 2);
insert( &head, 4);
insert( &head, 5);
insert( &head, 11);
insert( &head, 12);
insert( &head, 7);
insert( &head, 124);
insert( &head, 31);
count_prime(head);
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
//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 : ");
//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");
}
//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()
{
//Display linked list element
this.display();
// This are used to count prime nodes
int counter = 0;
//Get first node
Node temp = head;
System.out.print(" Prime Nodes : [");
//iterating linked list elements
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)
{
MyLinkedList obj = new MyLinkedList();
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
//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 : ";
//iterating linked list elements
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()
{
//Display linked list element
this->display();
// This are used to count prime nodes
int counter = 0;
//Get first node
Node * temp = this->head;
cout << " Prime Nodes : [";
//iterating linked list elements
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()
{
MyLinkedList obj = MyLinkedList();
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
//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 : ");
//iterating linked list elements
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()
{
//Display linked list element
this.display();
// This are used to count prime nodes
int counter = 0;
//Get first node
Node temp = head;
Console.Write(" Prime Nodes : [");
//iterating linked list elements
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)
{
MyLinkedList obj = new MyLinkedList();
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
//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 : ";
//iterating linked list elements
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()
{
//Display linked list element
$this->display();
// This are used to count prime nodes
$counter = 0;
//Get first node
$temp = $this->head;
echo " Prime Nodes : [";
//iterating linked list elements
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 = new MyLinkedList();
$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
//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 : ");
//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");
}
//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()
{
//Display linked list element
this.display();
// This are used to count prime nodes
var counter = 0;
//Get first node
var temp = this.head;
process.stdout.write(" Prime Nodes : [");
//iterating linked list elements
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()
{
var obj = new MyLinkedList();
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
# 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 = "")
# iterating linked list elements
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) :
# Display linked list element
self.display()
# This are used to count prime nodes
counter = 0
# Get first node
temp = self.head
print(" Prime Nodes : [", end = "")
# iterating linked list elements
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 = MyLinkedList()
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
# 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 : ")
# iterating linked list elements
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()
# Display linked list element
self.display()
# This are used to count prime nodes
counter = 0
# Get first node
temp = @head
print(" Prime Nodes : [")
# iterating linked list elements
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 = MyLinkedList.new()
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
//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 : ");
//iterating linked list elements
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 = {
//Display linked list element
this.display();
// This are used to count prime nodes
var counter: Int = 0;
//Get first node
var temp: Node = head;
print(" Prime Nodes : [");
//iterating linked list elements
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 = {
var obj: MyLinkedList = new MyLinkedList();
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
//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: "");
//iterating linked list elements
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()
{
//Display linked list element
self.display();
// This are used to count prime nodes
var counter: Int = 0;
//Get first node
var temp: Node? = self.head;
print(" Prime Nodes : [", terminator: "");
//iterating linked list elements
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()
{
let obj: MyLinkedList = MyLinkedList();
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
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