Delete all nodes which are divisible by X in doubly linked list

Assume that given doubly linked list are contain N nodes of integer values. Delete each nodes in this linked list which is divisible of given Value X. Suppose that linked list containing following (6, 1, 4, 9, 5, 3, 12 ) node in a sequence.

Before delete  nodes which are divisible by X After delete  nodes which are divisible by X

Here given code implementation process.

//C Program
//Delete all nodes from the doubly linked list which are divisible by X
#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*);
void delete_node(struct Node**,int);

//insert Node element of end of linked list
void insert(struct Node**head,int value){

  //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;
    if(*head==NULL){
      *head=node;
    }else{
        struct Node*temp=*head;
        //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){
    printf("Empty linked list");
  }
  else{
    printf("\nLinked List Elements :");
  
    //Traverse doubly linked list from front to rear
    while(temp!=NULL){
      //print node value
      printf("  %d",temp->data);
      temp=temp->next;
    }
  }

}

//Delete all nodes which are divisible by X
void delete_node(struct Node**head,int X){
  if(*head==NULL){
      printf("Empty linked list\n");
  }else{

      struct Node*temp=*head,*hold=NULL;

      while(temp!=NULL){

        //check deleted node
        if(temp->data % X == 0){
            hold=temp;
            if(temp==*head){
              //delete head node of linked list
              *head=temp->next;

            }
            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
  struct Node*head=NULL;
  int divisible=3;
  //Insert element of linked list
  insert(&head,6);
  insert(&head,1);
  insert(&head,4);
  insert(&head,9);
  insert(&head,5);
  insert(&head,3);
  insert(&head,12);
 
  printf("Before Delete Nodes : ");
  //display all node
  display(head);
  printf("\nAfter Delete all nodes which is divisible by %d ",divisible);
  delete_node(&head,divisible);

  //display all node
  display(head);
  return 0;
}

Output

Before Delete Nodes : 
Linked List Elements :  6  1  4  9  5  3  12
After Delete all nodes which is divisible by 3 
Linked List Elements :  1  4  5
//C++ Program
//Delete all nodes from the doubly linked list which are divisible by X

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

//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;
        if(head==NULL){
          head=node;
        }else{
            Node*temp=head;
            //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(){

    if(head==NULL){
        //when linked list is empty
        cout<<"Empty linked list";
    }
    else{
        cout<<"Linked List Element :";
        Node*temp=head;
        while(temp!=NULL){
            //display node elemement
            cout<<" "<<temp->data;
            temp=temp->next;
        }
    }

}

//Delete all nodes which are divisible by X
void  DoublyList:: delete_node(int X){
  if(head==NULL){
     cout<<"Empty linked list\n";
  }else{
     
      Node*temp=head,*hold=NULL;
      while(temp!=NULL){
        //check deleted node
        if(temp->data%X==0){
            hold=temp;
            if(temp==head){
              //delete head node of linked list
              head=temp->next;

            }
            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();
  int divisible=3;

  //Insert element of linked list
  obj.insert(6);
  obj.insert(1);
  obj.insert(4);
  obj.insert(9);
  obj.insert(5);
  obj.insert(3);
  obj.insert(12);
 
  cout<<"Before Delete Node  "<<endl;
    //display all node
  obj.display();
  cout<<"\nAfter Delete all nodes which is divisible by "<<divisible<<endl;
  obj.delete_node(divisible);
  //display all node
  obj.display();
  return 0;
}

Output

Before Delete Node  
Linked List Element : 6 1 4 9 5 3 12
After Delete all nodes which is divisible by 3
Linked List Element : 1 4 5
//Java program
//Delete all nodes from the doubly linked list which are divisible by X

public class LinkedList{

    static class Node{
        int data;
        Node next;
        Node prev;
    }
    //head of linked list
    static Node head;
    //Class constructors
    LinkedList(){
        head=null;
    } 
    //add newly created node at End of linked list
    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
        if(head==null) head=node;
        else{
          Node temp=head;
          //find last node
          while(temp.next!=null) temp=temp.next;
          //add node
          node.prev=temp;
          temp.next=node;
        }
    }
    //display all Linked List node value
    static void display(){
        if(head!=null){
            System.out.print(" Linked List Element :");
            Node temp=head;
            while(temp!=null){
                //display node value
                System.out.print("  "+temp.data);
                temp=temp.next;
            }
        }else{
            System.out.println("Empty Linked list"); 
        }
    }
    
    //Delete all nodes which are divisible by X
    public static void  deleteNode(int X){
       if(head==null){
          //when linked list is empty
          System.out.print("Empty linked list");
       }else{
        Node temp=head, hold=null;
        while(temp!=null){
          //check deleted node
          if(temp.data%X==0){
              hold=temp;
              if(temp==head){
                //delete head node of linked list
                head=temp.next;

              }
              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) {

        LinkedList obj=new LinkedList();
    
          int divisible=3;
          //Insert element of linked list
          obj.insert(6);
          obj.insert(1);
          obj.insert(4);
          obj.insert(9);
          obj.insert(5);
          obj.insert(3);
          obj.insert(12);
 
          System.out.println("Before delete nodes ");
            //display all node
          obj.display();
          System.out.println("\nAfter Delete all nodes which is divisible by "+divisible);
          obj.deleteNode(divisible);
          //display all node
          obj.display();
    }
}

Output

Before delete nodes 
 Linked List Element :  6  1  4  9  5  3  12
After Delete all nodes which is divisible by 3
 Linked List Element :  1  4  5
#Python Program
#Delete all nodes from the doubly linked list which are divisible by X
class Node:
    def __init__(self,data):
        self.data=data
        self.next=None
        self.prev=None

#create class LinkedList    
class LinkedList:
    def __init__(self):
        #set initial head value
        self.head=None


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

        if(self.head==None):
            #when empty list
            self.head=node
        else:
            temp=self.head
            #find last node
            while temp.next!=None:
                temp=temp.next
            #add node    
            temp.next=node
            node.prev=temp

    
    #Delete all nodes which are divisible by X
    def deleteNode(self,X):
        if self.head==None:
            print("Linked List Empty : ")
        else:
            temp=self.head

            while(temp!=None):
                #check deleted node
                if(temp.data%X==0):
                    hold=temp 
                    if(temp==self.head):
                        #delete head node of linked list
                        self.head=temp.next 

                    
                    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):
        if self.head==None:
            print("Linked List Empty : ")
            return
        temp=self.head
        print("Linked List Elements : ",end=" ")

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

    #Create Object of class LinkedList
    obj=LinkedList()
    divisible=3
    #Insert element of linked list
    obj.insert(6)
    obj.insert(1)
    obj.insert(4)
    obj.insert(9) 
    obj.insert(5) 
    obj.insert(3) 
    obj.insert(12) 

    print("Before delete nodes ") 
    #display all node
    obj.display() 
    print("\nAfter Delete all nodes which is divisible by ",divisible) 
    obj.deleteNode(divisible) 
    #display all node
    obj.display() 

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

Output

Before delete nodes 
Linked List Elements :  6 1 4 9 5 3 12 
After Delete all nodes which is divisible by  3
Linked List Elements :  1 4 5 
//C# Program
//Delete all nodes from the doubly linked list which are divisible by X

using System;
public class Node{
   public  int data;
   public  Node next;
   public Node prev;
}
class Program
{
    public Node head;
    public Program(){
        head=null;
    }
    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
        if(head==null) head=node;
        else{
          Node temp=head;
          //find last node
          while(temp.next!=null) temp=temp.next;
          //add node
          node.prev=temp;
          temp.next=node;
        }
    }

    //Delete all nodes which are divisible by X
    public void  deleteNode(int X){
       if(head==null){
          //when linked list is empty
             Console.Write("Empty linked list");
       }else{
         
         
        Node temp=head, hold=null;
        while(temp!=null){
          //check deleted node
          if(temp.data%X==0){
              hold=temp;
              if(temp==head){
                //delete head node of linked list
                head=temp.next;

              }
              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;
          }
        }
      }
    }

    //display linked list node value
    public void display(){
        if(head==null){
            Console.Write("Empty List");
        }else{
            Console.Write("\nLinked List :");
            Node temp=head;
            while(temp!=null){
                //display node value
                Console.Write("  {0}",temp.data);
                temp=temp.next;
            }
            
        }
    }
    
    static void Main()
    {
        Program obj=new Program();
        int divisible=3;
        //Insert element of linked list
        obj.insert(6);
        obj.insert(1);
        obj.insert(4);
        obj.insert(9);
        obj.insert(5);
        obj.insert(3);
        obj.insert(12);
       
        Console.Write("Before delete nodes  ");
          //display all node
        obj.display();
        Console.Write("\nAfter Delete all nodes which is divisible by {0}",divisible);
        obj.deleteNode(divisible);
        //display all node
        obj.display();

        
    }
}

Output

Before delete nodes 
Linked List : 6 1 4 9 5 3 12
After Delete all nodes which is divisible by 3
Linked List : 1 4 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







© 2021, kalkicode.com, All rights reserved