# Delete all Prime Nodes from a doubly linked list

Suppose we are inserted the following (7, 2, 3, 9, 5, 6, 11) node in a sequence.

Here given code implementation process.

//C Program
//Delete all Prime Nodes from a Doubly Linked List
#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data;
struct Node*next;
struct Node*prev;
};

//function prototype
void insert(struct Node**,int);
void display(struct Node*);
int check_prime(int);
void delete_node(struct Node**);

//insert Node element of end of linked list

//Create a dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
//set data value
node->data=value;
node->next=NULL;
node->prev=NULL;
}else{
//find last node
while(temp!=NULL && temp->next!=NULL){
temp=temp->next;
}
//add new node to last positions
temp->next=node;
node->prev=temp;

}
}
}
//display element of Node
void display(struct Node*temp){

if(temp==NULL){
}
else{

//Traverse doubly linked list from front to rear
while(temp!=NULL){
//print node value
printf("  %d",temp->data);
temp=temp->next;
}
}

}
//return given value is prime or not
int check_prime(int value){

if(value==1){
return 0;
}else{
unsigned int flag = 1;
for(int i = 2; i <= value/2; i++)
{
// None prime number
if(value%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
}
//delete all prime nodes
}else{
while(temp!=NULL){
//check deleted node
if(check_prime(temp->data)){
hold=temp;

}
if(temp->next!=NULL){
temp->next->prev=temp->prev;
}
if(temp->prev!=NULL){
temp->prev->next=temp->next;
}

temp=temp->next;
hold->next=NULL;
hold->prev=NULL;

free(hold);
hold=NULL;

}else{
temp=temp->next;
}
}
}
}

int main(){
//set node pointer value

printf("Before delete prime nodes ");
//display all node
printf("\nAfter delete prime nodes");

//display all node
return 0;
}

#### Output

Before delete prime nodes
Linked List Elements :  7  2  3  9  5  6  11
After delete prime nodes
Linked List Elements :  9  6
//C++ Program
//Delete all Prime Nodes from a Doubly Linked List

#include<iostream>
using namespace std;
//create structure
struct Node{
int data;
Node*next;
Node*prev;
};
class DoublyList{
public:
DoublyList();
void insert(int);
void display();
void delete_node();
int check_prime(int);
};
//set inital value
DoublyList::DoublyList(){
}

//insert a linked list Node element
void DoublyList:: insert(int value){
//Create a dynamic node
Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow\n";
}
else{
//set data value
node->data=value;
node->next=NULL;
node->prev=NULL;
}else{
//find last node
while(temp!=NULL && temp->next!=NULL){
temp=temp->next;
}
//add new node to last positions
temp->next=node;
node->prev=temp;

}
}
}
//display all element of doubly linked list
void DoublyList::display(){

}
else{
while(temp!=NULL){
//display node elemement
cout<<" "<<temp->data;
temp=temp->next;
}
}

}
//return given value is prime or not
int  DoublyList:: check_prime(int value){

if(value==1){
return 0;
}else{
unsigned int flag = 1;
for(int i = 2; i <= value/2; i++)
{
// None prime number
if(value%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
}
//remove all prime nodes
void  DoublyList:: delete_node(){
}else{

while(temp!=NULL){
//check deleted node
if(check_prime(temp->data)){
hold=temp;

}
if(temp->next!=NULL){
temp->next->prev=temp->prev;
}
if(temp->prev!=NULL){
temp->prev->next=temp->next;
}

temp=temp->next;
hold->next=NULL;
hold->prev=NULL;

delete hold;
hold=NULL;

}else{
temp=temp->next;
}
}

}
}

int main(){
DoublyList obj=DoublyList();

obj.insert(7);
obj.insert(2);
obj.insert(3);
obj.insert(9);
obj.insert(5);
obj.insert(6);
obj.insert(11);

cout<<"Before delete prime node  "<<endl;
//display all node
obj.display();
cout<<"\nAfter delete prime node "<<endl;
obj.delete_node();
//display all node
obj.display();
return 0;
}

#### Output

Before delete prime node
Linked List Element : 7 2 3 9 5 6 11
After delete prime node
Linked List Element : 9 6
//Java program
//Delete all Prime Nodes from a Doubly Linked List

static class Node{
int data;
Node next;
Node prev;
}
//Class constructors
}
static void insert(int value){
//Create a dynamic node
Node node=new Node();
//add node value and pointer value
node.data=value;
node.next=null;
node.prev=null;
//when no element
else{
//find last node
while(temp.next!=null) temp=temp.next;
node.prev=temp;
temp.next=node;
}
}
//display all Linked List node value
static void display(){
while(temp!=null){
//display node value
System.out.print("  "+temp.data);
temp=temp.next;
}
}else{
}
}
//return given value is prime or not
static int  checkPrime(int value){

if(value==1){
return 0;
}else{
int flag = 1;
for(int i = 2; i <= value/2; i++)
{
// None prime number
if(value%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
}
//delete all prime element
public static void  deleteNode(){
}else{
while(temp!=null){
//check deleted node
if(checkPrime(temp.data)==1){
hold=temp;

}
if(temp.next!=null){
temp.next.prev=temp.prev;
}
if(temp.prev!=null){
temp.prev.next=temp.next;
}

temp=temp.next;
hold.next=null;
hold.prev=null;

hold=null;

}else{
temp=temp.next;
}
}

}
}

public static void main(String[] args) {

obj.insert(7);
obj.insert(2);
obj.insert(3);
obj.insert(9);
obj.insert(5);
obj.insert(6);
obj.insert(11);

System.out.println("Before delete prime node ");
//display all node
obj.display();
System.out.println("\nAfter delete prime node ");
obj.deleteNode();
//display all node
obj.display();
}
}

#### Output

Before delete prime node
Linked List Element :  7  2  3  9  5  6  11
After delete prime node
Linked List Element :  9  6
#Python Program
#Delete all Prime Nodes from a Doubly
class Node:
def __init__(self,data):
self.data=data
self.next=None
self.prev=None

def __init__(self):

#insert new node to end of Linked List
def insert(self,data):
#make a new node
node=Node(data)

#when empty list
else:
#find last node
while temp.next!=None:
temp=temp.next
temp.next=node
node.prev=temp

def checkPrime(self,value):

if(value==1):
return 0
else:
flag = 1
i=2
while(i <= value/2):
if(value%i == 0):
flag = 0
break
i+=1
return flag

#delete all prime nodes
def deleteNode(self):
else:

while(temp!=None):
#check deleted node
if(self.checkPrime(temp.data)==1):
hold=temp;

if(temp.next!=None):
temp.next.prev=temp.prev;

if(temp.prev!=None):
temp.prev.next=temp.next;

temp=temp.next;
hold.next=None;
hold.prev=None;
hold=None;

else:
temp=temp.next;

#view all node values
def display(self):
return
print("Linked List Elements : ",end=" ")

while(temp!=None):
#print node value
print(temp.data,end=" ")
temp=temp.next

def main():

obj.insert(7);
obj.insert(2);
obj.insert(3);
obj.insert(9);
obj.insert(5);
obj.insert(6);
obj.insert(11);

print("Before delete prime node ");
#display all node
obj.display();
print("\nAfter delete prime node ");
obj.deleteNode();
#display all node
obj.display();

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

#### Output

Before delete prime node
Linked List Elements :  7 2 3 9 5 6 11
After delete prime node
Linked List Elements :  9 6
//C# Program
//Delete all Prime Nodes from a Doubly Linked List
using System;
public class Node{
public  int data;
public  Node next;
public Node prev;
}
class Program
{
public Program(){
}
public void insert(int data){
//Create a dynamic node
Node node=new Node();
//add node value and pointer value
node.data=data;
node.next=null;
node.prev=null;
//when no element
else{
//find last node
while(temp.next!=null) temp=temp.next;
node.prev=temp;
temp.next=node;
}
}
public int  checkPrime(int value){

if(value==1){
return 0;
}else{
int flag = 1;
for(int i = 2; i <= value/2; i++)
{
// None prime number
if(value%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
}
//delete all prime node
public void  deleteNode(){
}else{

while(temp!=null){
//check deleted node
if(checkPrime(temp.data)==1){
hold=temp;

}
if(temp.next!=null){
temp.next.prev=temp.prev;
}
if(temp.prev!=null){
temp.prev.next=temp.next;
}

temp=temp.next;
hold.next=null;
hold.prev=null;

hold=null;

}else{
temp=temp.next;
}
}
}
}

public void display(){
Console.Write("Empty List");
}else{
while(temp!=null){
//display node value
Console.Write("  {0}",temp.data);
temp=temp.next;
}

}
}

static void Main()
{
Program obj=new Program();

obj.insert(7);
obj.insert(2);
obj.insert(3);
obj.insert(9);
obj.insert(5);
obj.insert(6);
obj.insert(11);

Console.Write("Before delete prime node  ");
//display all node
obj.display();
Console.Write("\nAfter delete prime node");
obj.deleteNode();
//display all node
obj.display();

}
}

#### Output

Before delete prime node
Linked List : 7 2 3 9 5 6 11
After delete prime node