Delete all the even nodes of a Circular Linked List | CPP program to delete all even node from a Circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not NULL then set the next of last node ; For the first node ; Delete the node if it is even ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Finally , free the memory occupied by del ; Function to delete all even nodes from the singly circular linked list ; traverse list till the end if the node is even then delete it ; if node is even ; point to next node ; Function to print nodes ; Driver code ; Initialize lists as empty ; Created linked list will be 57 -> 11 -> 2 -> 56 -> 12 -> 61
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int data ) { struct Node * ptr1 = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; struct Node * temp = * head_ref ; ptr1 -> data = data ; ptr1 -> next = * head_ref ; if ( * head_ref != NULL ) { while ( temp -> next != * head_ref ) temp = temp -> next ; temp -> next = ptr1 ; } else ptr1 -> next = ptr1 ; * head_ref = ptr1 ; } void deleteNode ( Node * head_ref , Node * del ) { struct Node * temp = head_ref ; if ( head_ref == del ) head_ref = del -> next ; while ( temp -> next != del ) { temp = temp -> next ; } temp -> next = del -> next ; free ( del ) ; return ; } void deleteEvenNodes ( Node * head ) { struct Node * ptr = head ; struct Node * next ; do { if ( ptr -> data % 2 == 0 ) deleteNode ( head , ptr ) ; next = ptr -> next ; ptr = next ; } while ( ptr != head ) ; } void printList ( struct Node * head ) { struct Node * temp = head ; if ( head != NULL ) { do { printf ( " % d ▁ " , temp -> data ) ; temp = temp -> next ; } while ( temp != head ) ; } } int main ( ) { struct Node * head = NULL ; push ( & head , 61 ) ; push ( & head , 12 ) ; push ( & head , 56 ) ; push ( & head , 2 ) ; push ( & head , 11 ) ; push ( & head , 57 ) ; cout << " List after deletion : " ; deleteEvenNodes ( head ) ; printList ( head ) ; return 0 ; }