Delete all Even position nodes from circular linked list

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

Before Delete all Even position nodes in linked list After Delete all Even position nodes in linked list

Here given code implementation process.

//C Program 
//Delete all even position nodes from circular linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function

//Create structure
struct Node{
  int data;
  struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
//insert Node element at end of linked list
void insert(struct Node**head,int value){
  //Create dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL){
    printf("Memory overflow\n");
  }else{
    node->data=value;
    node->next=*head;
    if(*head==NULL){
      *head=node;
      node->next=*head;
    }else{
      struct Node*temp=*head;
      while(temp->next!=*head){
        temp=temp->next;
      }
      //Add node at last
      temp->next=node;
    }
  }
}

//Display element of Node
void display(struct Node*head){

  if(head==NULL){
    printf("\nEmpty linked list");
  }
  else{
    printf("\n Linked List Element :");
    struct Node*temp=head;
    while(temp){
      printf("  %d",temp->data);
      temp=temp->next;
      if(temp==head){
        break; //terminate loop
      }
    }

  }
}


//Deleted node which are exist in 
//even position of linked list
void del_even(struct Node**head){

  if(*head!=NULL){
    struct Node*temp=*head,*last_node=*head,*hold=NULL,*prev=NULL;
    int position=0,counter=0;
    //find last node
    do{
      last_node=temp;
      temp=temp->next;
      counter++;
    }while(temp!=*head);

      while(counter>0){
        if(position%2==0){
          hold=temp;
        }else{
          prev=temp;
        }
        position++;
        temp=temp->next;
        if(hold!=NULL){
          if(hold==*head) *head=temp;
          if(last_node!=NULL && last_node!=hold){
            last_node->next=*head;
          }else{
            //when delete last node of circular linked list
            last_node=NULL;
          }
          if(prev!=NULL){
            prev->next=temp;
          }
          free(hold);
          hold=NULL;
        }
        counter--;
      }
    }else{
      printf("\n Empty Linked List");
    }
  }
  int main(){
    //create node pointer
    struct Node*head=NULL;
    //insert element of linked list
    insert(&head,2);
    insert(&head,3);
    insert(&head,1);
    insert(&head,4);
    insert(&head,5);
    insert(&head,6);
    insert(&head,7);
  

    
    printf("\n Before Delete even position Nodes");
    //display all node
    display(head);
    printf("\n After Delete even position Nodes");
    del_even(&head);
    display(head);
    return 0;
  }

Output

 Before Delete even position Nodes
 Linked List Element :  2  3  1  4  5  6  7
 After Delete even position Nodes
 Linked List Element :  3  4  6
//C++ Program
//Delete all even position nodes from circular linked list
#include<iostream>
using namespace std;

//create structure of linked list node
class Node{
public:
  int data;
  Node*next;
};
class CircularList{
  Node *head;
public:
  CircularList();
  void insert(int);
  void display();
  void del_even();
};
CircularList::CircularList(){
  head=NULL;
}

//insert Node at end of linked list 
void CircularList:: insert(int value){
  //Create dynamic node
   Node*node=new Node;
  if(node==NULL){
    cout<<"Memory overflow"<<endl;
  }else{
    node->data=value;
    node->next=head;
    if(head==NULL){
      //when linked list empty
      head=node;
      node->next=head;
    }else{
      Node*temp=head;
      //find last node
      while(temp->next!=head){
        temp=temp->next;
      }
      //Add node at last
      temp->next=node;
    }
  }
}
//display element of Node
void CircularList:: display(){
  
  if(head==NULL){
    cout<<"Empty linked list";
  }
  else{
 
    Node*temp=head;
    while(temp){
      cout<<"  "<<temp->data;
      temp=temp->next;
      if(temp==head){
        break; //terminate loop
      }
    }

  }
}
//Deleted node which are exist in 
//even position of linked list
void CircularList:: del_even(){

  if(head!=NULL){
     Node*temp=head,*last_node=head,*hold=NULL,*prev=NULL;
    int position=0,counter=0;
    //find last node
    do{
      last_node=temp;
      temp=temp->next;
      counter++;
    }while(temp!=head);

      while(counter>0){
        if(position%2==0){
          hold=temp;
        }else{
          prev=temp;
        }
        position++;
        temp=temp->next;
        if(hold!=NULL){
          if(hold==head) head=temp;
          if(last_node!=NULL && last_node!=hold){
            last_node->next=head;
          }else{
            //when delete last node of circular linked list
            last_node=NULL;
          }
          if(prev!=NULL){
            prev->next=temp;
          }
          delete hold;
          hold=NULL;
        }
        counter--;
      }
    }else{
      cout<<"\n Empty Linked List";
    }
}
int main(){
  CircularList obj=CircularList();
  //insert element of linked list
  obj.insert(2);
  obj.insert(3);
  obj.insert(1);
  obj.insert(4);
  obj.insert(5);
  obj.insert(6);
  obj.insert(7);

  cout<<"\n Before Delete even position Nodes";
  //display all node
  obj.display();
  cout<<"\n After Delete even position Nodes";
  obj.del_even();
  obj.display();
  return 0;
}

Output

 Before Delete even position Nodes  2  3  1  4  5  6  7
 After Delete even position Nodes  3  4  6
//Java Program to 
//Delete all even position nodes from circular linked list
public class LinkedList{
  static class Node{
    int data;
    Node next;
  }
  static Node head;
    //Class constructors
  LinkedList(){
    head=null;
  } 
    //insert node at last of linke list
  static void insert(int value){
      //Create a node
    Node node=new Node();
    node.data=value;
    node.next=head;
    if(head==null){
      head=node;
      node.next=head;
    }
    else{
      Node temp=head;
          //find lase node
      while(temp.next!=head){
        temp=temp.next;
      }
          //add node
      temp.next=node;
    }

  }
    //Deleted node which are exist in 
  //even position of linked list
  public void  del_even(){

    if(head!=null){
      Node temp=head, last_node=head, hold=null, prev=null;
      int position=0,counter=0;
        //find last node
      do{
        last_node=temp;
        temp=temp.next;
        counter++;
      }while(temp!=head);

      while(counter>0){
        if(position%2==0){
          hold=temp;
        }else{
          prev=temp;
        }
        position++;
        temp=temp.next;
        if(hold!=null){
          if(hold==head) head=temp;
          if(last_node!=null && last_node!=hold){
            last_node.next=head;
          }else{
                  //when delete last node of circular linked list
            last_node=null;
          }
          if(prev!=null){
            prev.next=temp;
          }

          hold=null;
        }
        counter--;
      }
    }else{
      System.out.println("\n Empty Linked List");
    }
  }
    //Display node element of circular linked list
  public static void display(){
    if(head==null){
      System.out.println("Empty Linked List");
    }else{
      
      Node temp=head;
      while(temp!=null){
        System.out.print("  "+temp.data);
        temp=temp.next;
        if(temp==head) break;
      }
    }
  }



  public static void main(String[] args) {
    LinkedList obj=new LinkedList();
    //insert element of linked list
    obj.insert(2);
    obj.insert(3);
    obj.insert(1);
    obj.insert(4);
    obj.insert(5);
    obj.insert(6);
    obj.insert(7);

    System.out.print("\n Before Delete even position Nodes");
    //display all node
    obj.display();
    System.out.print("\n After Delete even position Nodes");
    obj.del_even();
    obj.display();

  }
}

Output

 Before Delete even position Nodes  2  3  1  4  5  6  7
 After Delete even position Nodes  3  4  6
#Python Program
#Delete all even position nodes from circular linked list
class Node:
  def __init__(self,data):
    self.data=data
    self.next=None

#create class CircularList    
class CircularList:
  def __init__(self):
    #Assign default value
    self.head=None
    
  #Insert new node to End of Linked list  
  def insert(self,data):
    node=Node(data)
    node.next=self.head
    if(self.head==None):
        #when no element of linked list
        self.head=node
        node.next=node
    else:
        temp=self.head
        while(temp.next!=self.head):
            temp=temp.next
        #add node    
        temp.next=node    
  #display all element of circular linked list      
  def display(self):
    temp=self.head
    while(temp!=None):
      print(temp.data,end=" ")
      temp=temp.next
      #when find first node
      if(temp==self.head):
        break

  
  #Deleted node which are exist in 
  #even position of linked list
  def  deleteEvenPosition(self):

    if(self.head!=None):
        temp=self.head.next
        last_node=self.head
        hold=None
        prev=None
        position=0
        counter=1
        #find last node
        while temp!=self.head:
            last_node=temp
            temp=temp.next
            counter+=1
        temp=self.head
        while(counter>0):
            if(position%2==0):
                hold=temp
               
            else:
                prev=temp
                
            position=position+1
            temp=temp.next
            if(hold!=None):

                if(hold==self.head):
                    self.head=temp

                if(last_node!=None and last_node!=hold):
                    last_node.next=self.head
                else:
                    #when delete last node of circular linked list
                    last_node=None
                    
                if(prev!=None):
                    prev.next=temp
                    

                hold=None
            counter-=1
            
    else:
        print("\n Empty Linked List")
        
    

def main():
    #Create Object of class CircularList
    obj=CircularList()
    obj.insert(2)
    obj.insert(3)
    obj.insert(1)
    obj.insert(4)
    obj.insert(5)
    obj.insert(6)
    obj.insert(7)

    print("\n Before Delete even position Nodes")
    #display all node
    obj.display()
    print("\n After Delete even position Nodes")
    obj.deleteEvenPosition()
    obj.display() 

if __name__ == '__main__':
  main()

Output

 Before Delete even position Nodes
2 3 1 4 5 6 7 
 After Delete even position Nodes
3 4 6
//C# Program to 
//Delete all even position nodes from circular linked list
using System;
//node class
public class Node{
  public  int data;
  public  Node next;
}
class LinkedList
{
  public Node head;
  public LinkedList(){
    head=null;
  }
  //insert node of linked list
  public void insert(int data){
    Node newNode=new Node();
    newNode.data=data;
    newNode.next=head;
    if(head==null){
      //when no element of linked list
      head=newNode;
      newNode.next=head;
    }
    else{
      Node temp=head;
      //get last node
      while(temp.next!=head ){
        temp=temp.next;
      }
      //add new node
      temp.next=newNode;
    }
  }
  //Deleted node which are exist in 
  //even position of linked list
  public void  deleteEvenPosition(){

    if(head!=null){
      Node temp=head, last_node=head, hold=null, prev=null;
      int position=0,counter=0;
      //find last node
      do{
        last_node=temp;
        temp=temp.next;
        counter++;
      }while(temp!=head);

      while(counter>0){
        if(position%2==0){
          hold=temp;
        }else{
          prev=temp;
        }
        position++;
        temp=temp.next;
        if(hold!=null){
          if(hold==head) head=temp;
          if(last_node!=null && last_node!=hold){
            last_node.next=head;
          }else{
            //when delete last node of circular linked list
            last_node=null;
          }
          if(prev!=null){
            prev.next=temp;
          }

          hold=null;
        }
        counter--;
      }
    }else{
      Console.Write("\n Empty Linked List");
    }
  }
  //Display element of linked list
  public void display(){
    if(head==null){
      Console.Write("Empty Linked List");
    }else{
      
      Node temp=head;
      while(temp!=null){
        Console.Write(" {0}",temp.data);
        temp=temp.next;
        //End of Loop iteration 
        if(temp==head) break;
      }
    }
  }


  static void Main(){

    LinkedList obj=new LinkedList();
    //insert element of linked list
    obj.insert(2);
    obj.insert(3);
    obj.insert(1);
    obj.insert(4);
    obj.insert(5);
    obj.insert(6);
    obj.insert(7);

    Console.Write("\n Before Delete even position Nodes");
    //display all node
    obj.display();
    Console.Write("\n After Delete even position Nodes");
    obj.deleteEvenPosition();
    obj.display();
  }
}

Output

 Before Delete even position Nodes 2 3 1 4 5 6 7
 After Delete even position Nodes 3 4 6

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