# Delete all non prime nodes in linked list

Here given code implementation process.

``````//C Program
//Delete all non prime nodes in linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

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
{
struct Node *node = create_node(data);
if ( *head == NULL)
{
}
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)
{
return;
}
struct Node *temp = head;
//iterating linked list elements
while (temp != NULL)
{
if (temp != head)
{
printf(" →");
}
printf(" %d", temp->data);
//visit to next node
temp = temp->next;
}
printf(" → NULL\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;
}
// This are deleting all non prime node in linked list
void delete_non_prime(struct Node ** head)
{

if (*head == NULL)
{
}
else
{
// Define some auxiliary variable

struct Node*auxiliary = *head;

struct Node*back = NULL;

struct Node*temp = NULL;

// iterating linked list elements
while(auxiliary != NULL)
{
if(!is_prime(auxiliary->data))
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary->next;

if(temp!=NULL)
{

{
}
else
{
back->next = auxiliary;
}
//free node
temp->next=NULL;
free(temp);
temp=NULL;
}
}
}

}
int main()
{

struct Node *head = NULL;

printf(" Before Delete None-Prime nodes\n");
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL

// Perform node deletion operation

printf(" After Delete None-Prime nodes\n");
// 7 → 2 → 23 → 37 → NULL
return 0;
}``````

#### Output

`````` Before Delete None-Prime nodes
10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
7 → 2 → 23 → 37 → NULL``````
``````// Java Program
// Delete all non prime nodes in 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 constructor
{
this.tail = null;
}
//insert node at last of linke list
public void add_node(int data)
{
//Create a node
Node node = new Node(data);
if (this.head == null)
{
//When linked list empty add first 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)
{
return;
}
Node temp = this.head;
System.out.print("Linked List : ");
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print(" → NULL\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;
}
// This are deleting all non prime node in linked list
public void delete_non_prime()
{
if (this.head == null)
{
}
else
{
// Define some auxiliary variable
Node auxiliary = this.head;
Node back = null;
Node temp = null;
// iterating linked list elements
while (auxiliary != null)
{
if (is_prime(auxiliary.data) == false)
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary.next;
if (temp != null)
{
if (temp == this.head)
{
}
else
{
back.next = auxiliary;
}
//free node
temp.next = null;
temp = null;
}
}
}
}
public static void main(String[] args)
{
System.out.print(" Before Delete None-Prime nodes\n");
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display();
// Perform node deletion operation
obj.delete_non_prime();
System.out.print(" After Delete None-Prime nodes\n");
// 7 → 2 → 23 → 37 → NULL
obj.display();
}
}``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Delete all non prime nodes in linked list

class Node
{
public: int data;
Node *next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node *tail;
//Class constructor
{
this->tail = NULL;
}
//insert node at last of linke list
{
//Create a node
Node *node = new Node(data);
if (this->head == NULL)
{
//When linked list empty add first 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 << "Linked List : ";
//iterating linked list elements
while (temp != NULL)
{
if (temp != this->head)
{
cout << " →";
}
cout << " " << temp->data;
//visit to next node
temp = temp->next;
}
cout << " → NULL\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;
}
// This are deleting all non prime node in linked list
void delete_non_prime()
{
if (this->head == NULL)
{
cout << "\nEmpty linked List";
}
else
{
// Define some auxiliary variable
Node *auxiliary = this->head;
Node *back = NULL;
Node *temp = NULL;
// iterating linked list elements
while (auxiliary != NULL)
{
if (this->is_prime(auxiliary->data) == false)
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary->next;
if (temp != NULL)
{
if (temp == this->head)
{
}
else
{
back->next = auxiliary;
}
//free node
temp->next = NULL;
delete(temp);
temp = NULL;
}
}
}
}
};
int main()
{
cout << " Before Delete None-Prime nodes\n";
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display();
// Perform node deletion operation
obj.delete_non_prime();
cout << " After Delete None-Prime nodes\n";
// 7 → 2 → 23 → 37 → NULL
obj.display();
return 0;
}``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL``````
``````//Include namespace system
using System;

// C# Program
// Delete all non prime nodes in 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 constructor
{
this.tail = null;
}
//insert node at last of linke list
public void add_node(int data)
{
//Create a node
Node node = new Node(data);
if (this.head == null)
{
//When linked list empty add first 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)
{
return;
}
Node temp = this.head;
Console.Write("Linked List : ");
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write(" → NULL\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;
}
// This are deleting all non prime node in linked list
public void delete_non_prime()
{
if (this.head == null)
{
}
else
{
// Define some auxiliary variable
Node auxiliary = this.head;
Node back = null;
Node temp = null;
// iterating linked list elements
while (auxiliary != null)
{
if (is_prime(auxiliary.data) == false)
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary.next;
if (temp != null)
{
if (temp == this.head)
{
}
else
{
back.next = auxiliary;
}
//free node
temp.next = null;
temp = null;
}
}
}
}
public static void Main(String[] args)
{
Console.Write(" Before Delete None-Prime nodes\n");
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display();
// Perform node deletion operation
obj.delete_non_prime();
Console.Write(" After Delete None-Prime nodes\n");
// 7 → 2 → 23 → 37 → NULL
obj.display();
}
}``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL``````
``````<?php
// Php Program
// Delete all non prime nodes in linked list

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

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructor
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
{
//Create a node
\$node = new Node(\$data);
if (\$this->head == null)
{
//When linked list empty add first 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;
}
echo "Linked List : ";
//iterating linked list elements
while (\$temp != null)
{
if (\$temp != \$this->head)
{
echo " →";
}
echo " ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo " → NULL\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;
}
// This are deleting all non prime node in linked list
public	function delete_non_prime()
{
if (\$this->head == null)
{
echo "\nEmpty linked List";
}
else
{
// Define some auxiliary variable
\$back = null;
\$temp = null;
// iterating linked list elements
while (\$auxiliary != null)
{
if (\$this->is_prime(\$auxiliary->data) == false)
{
//When node value is not prime
\$temp = \$auxiliary;
}
else
{
\$back = \$auxiliary;
}
//Visit to next node
\$auxiliary = \$auxiliary->next;
if (\$temp != null)
{
if (\$temp == \$this->head)
{
}
else
{
\$back->next = \$auxiliary;
}
//free node
\$temp->next = null;
\$temp = null;
}
}
}
}
}

function main()
{
\$obj = new MyLinkedList();
echo " Before Delete None-Prime nodes\n";
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
\$obj->display();
// Perform node deletion operation
\$obj->delete_non_prime();
echo " After Delete None-Prime nodes\n";
// 7 → 2 → 23 → 37 → NULL
\$obj->display();
}
main();``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL``````
``````// Node Js Program
// Delete all non prime nodes in linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructor
constructor()
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
var node = new Node(data);
if (this.head == null)
{
//When linked list empty add first 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)
{
return;
}
var temp = this.head;
process.stdout.write("Linked List : ");
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\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;
}
// This are deleting all non prime node in linked list
delete_non_prime()
{
if (this.head == null)
{
}
else
{
// Define some auxiliary variable
var auxiliary = this.head;
var back = null;
var temp = null;
// iterating linked list elements
while (auxiliary != null)
{
if (this.is_prime(auxiliary.data) == false)
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary.next;
if (temp != null)
{
if (temp == this.head)
{
}
else
{
back.next = auxiliary;
}
//free node
temp.next = null;
temp = null;
}
}
}
}
}

function main()
{
var obj = new MyLinkedList();
process.stdout.write(" Before Delete None-Prime nodes\n");
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display();
// Perform node deletion operation
obj.delete_non_prime();
process.stdout.write(" After Delete None-Prime nodes\n");
// 7 → 2 → 23 → 37 → NULL
obj.display();
}
main();``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL``````
``````#  Python 3 Program
#  Delete all non prime nodes in linked list

# Node of LinkedList
class Node :

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

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

# insert node at last of linke list
def add_node(self, data) :
# Create a node
node = Node(data)
if (self.head == None) :
# When linked list empty add first 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

print("Linked List : ", end = "")
# iterating linked list elements
while (temp != None) :
if (temp != self.head) :
print(" →", end = "")

print(" ", temp.data, end = "")
# visit to next node
temp = temp.next

print(" → NULL\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

#  This are deleting all non prime node in linked list
def delete_non_prime(self) :
if (self.head == None) :
print("\nEmpty linked List", end = "")
else :
#  Define some auxiliary variable
back = None
temp = None
#  iterating linked list elements
while (auxiliary != None) :
if (self.is_prime(auxiliary.data) == False) :
# When node value is not prime
temp = auxiliary
else :
back = auxiliary

# Visit to next node
auxiliary = auxiliary.next
if (temp != None) :
if (temp == self.head) :
else :
back.next = auxiliary

# free node
temp.next = None
temp = None

def main() :
print(" Before Delete None-Prime nodes\n", end = "")
#  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display()
#  Perform node deletion operation
obj.delete_non_prime()
print(" After Delete None-Prime nodes\n", end = "")
#  7 → 2 → 23 → 37 → NULL
obj.display()

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

#### Output

`````` Before Delete None-Prime nodes
Linked List :   10 →  7 →  1 →  8 →  2 →  4 →  23 →  37 → NULL
After Delete None-Prime nodes
Linked List :   7 →  2 →  23 →  37 → NULL``````
``````#  Ruby Program
#  Delete all non prime nodes in linked list

# Node of LinkedList
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

# Define the accessor and reader of class MyLinkedList

# Class constructor
def initialize()

self.tail = nil
end
# insert node at last of linke list

# Create a node
node = Node.new(data)
if (self.head == nil)

# When linked list empty add first 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)

return
end
print("Linked List : ")
# iterating linked list elements
while (temp != nil)

if (temp != self.head)

print(" →")
end
print(" ", temp.data)
# visit to next node
temp = temp.next
end
print(" → NULL\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
#  This are deleting all non prime node in linked list
def delete_non_prime()

if (self.head == nil)

else

#  Define some auxiliary variable
back = nil
temp = nil
#  iterating linked list elements
while (auxiliary != nil)

if (self.is_prime(auxiliary.data) == false)

# When node value is not prime
temp = auxiliary
else

back = auxiliary
end
# Visit to next node
auxiliary = auxiliary.next
if (temp != nil)

if (temp == self.head)

else

back.next = auxiliary
end
# free node
temp.next = nil
temp = nil
end
end
end
end
end
def main()

print(" Before Delete None-Prime nodes\n")
#  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display()
#  Perform node deletion operation
obj.delete_non_prime()
print(" After Delete None-Prime nodes\n")
#  7 → 2 → 23 → 37 → NULL
obj.display()
end
main()``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL
``````
``````// Scala Program
// Delete all non prime nodes in linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructor
def this()
{
this(null, null);
}
//insert node at last of linke list
def add_node(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
if (this.head == null)
{
//When linked list empty add first 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)
{
return;
}
var temp: Node = this.head;
print("Linked List : ");
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
print(" →");
}
print(" " + temp.data);
//visit to next node
temp = temp.next;
}
print(" → NULL\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;
}
// This are deleting all non prime node in linked list
def delete_non_prime(): Unit = {
if (this.head == null)
{
}
else
{
// Define some auxiliary variable
var auxiliary: Node = this.head;
var back: Node = null;
var temp: Node = null;
// iterating linked list elements
while (auxiliary != null)
{
if (is_prime(auxiliary.data) == false)
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary.next;
if (temp != null)
{
if (temp == this.head)
{
}
else
{
back.next = auxiliary;
}
//free node
temp.next = null;
temp = null;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
print(" Before Delete None-Prime nodes\n");
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display();
// Perform node deletion operation
obj.delete_non_prime();
print(" After Delete None-Prime nodes\n");
// 7 → 2 → 23 → 37 → NULL
obj.display();
}
}``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :  10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
After Delete None-Prime nodes
Linked List :  7 → 2 → 23 → 37 → NULL``````
``````// Swift 4 Program
// Delete all non prime nodes in linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var head: Node? ;
var tail: Node? ;
//Class constructor
init()
{
self.tail = nil;
}
//insert node at last of linke list
func add_node(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
if (self.head == nil)
{
//When linked list empty add first 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("Linked List : ", terminator: "");
//iterating linked list elements
while (temp != nil)
{
if (!(temp === self.head))
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print(" → NULL\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;
}
// This are deleting all non prime node in linked list
func delete_non_prime()
{
if (self.head == nil)
{
print("\nEmpty linked List", terminator: "");
}
else
{
// Define some auxiliary variable
var auxiliary: Node? = self.head;
var back: Node? = nil;
var temp: Node? = nil;
// iterating linked list elements
while (auxiliary != nil)
{
if (self.is_prime(auxiliary!.data) == false)
{
//When node value is not prime
temp = auxiliary;
}
else
{
back = auxiliary;
}
//Visit to next node
auxiliary = auxiliary!.next;
if (temp != nil)
{
if (temp === self.head)
{
}
else
{
back!.next = auxiliary;
}
//free node
temp!.next = nil;
temp = nil;
}
}
}
}
}
func main()
{
print(" Before Delete None-Prime nodes\n", terminator: "");
// 10 → 7 → 1 → 8 → 2 → 4 → 23 → 37 → NULL
obj.display();
// Perform node deletion operation
obj.delete_non_prime();
print(" After Delete None-Prime nodes\n", terminator: "");
// 7 → 2 → 23 → 37 → NULL
obj.display();
}
main();``````

#### Output

`````` Before Delete None-Prime nodes
Linked List :   10 →  7 →  1 →  8 →  2 →  4 →  23 →  37 → NULL
After Delete None-Prime nodes
Linked List :   7 →  2 →  23 →  37 → NULL``````

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.