Remove all even parity nodes from a Doubly and Circular Singly Linked List | C ++ implementation to remove all the Even Parity Nodes from a doubly linked list ; Node of the doubly linked list ; Function to insert a node at the beginning of the Doubly Linked List ; Allocate the node ; Insert the data ; Since we are adding at the beginning , prev is always NULL ; Link the old list off the new node ; Change the prev of head node to new node ; Move the head to point to the new node ; Function that returns true if count of set bits in x is even ; parity will store the count of set bits ; Function to delete a node in a Doubly Linked List . head_ref -- > pointer to head node pointer . del -- > pointer to node to be deleted ; Base case ; If the node to be deleted is head node ; Change next only if node to be deleted is not the last node ; Change prev only if node to be deleted is not the first node ; Finally , free the memory occupied by del ; Function to to remove all the Even Parity Nodes from a doubly linked list ; Iterating through the linked list ; If node ' s ▁ data ' s parity is even ; Function to print nodes in a given doubly linked list ; Driver Code ; Create the doubly linked list 18 < -> 15 < -> 8 < -> 9 < -> 14 ; Uncomment to view the list cout << " Original ▁ List : ▁ " ; printList ( head ) ; ; Modified List
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * prev , * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = ( Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> prev = NULL ; new_node -> next = ( * head_ref ) ; if ( ( * head_ref ) != NULL ) ( * head_ref ) -> prev = new_node ; ( * head_ref ) = new_node ; } bool isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x & 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } void deleteNode ( Node * * head_ref , Node * del ) { if ( * head_ref == NULL del == NULL ) return ; if ( * head_ref == del ) * head_ref = del -> next ; if ( del -> next != NULL ) del -> next -> prev = del -> prev ; if ( del -> prev != NULL ) del -> prev -> next = del -> next ; free ( del ) ; return ; } void deleteEvenParityNodes ( Node * * head_ref ) { Node * ptr = * head_ref ; Node * next ; while ( ptr != NULL ) { next = ptr -> next ; if ( isEvenParity ( ptr -> data ) ) deleteNode ( head_ref , ptr ) ; ptr = next ; } } void printList ( Node * head ) { if ( head == NULL ) { cout << " Empty ▁ list STRNEWLINE " ; return ; } while ( head != NULL ) { cout << head -> data << " ▁ " ; head = head -> next ; } } int main ( ) { Node * head = NULL ; push ( & head , 14 ) ; push ( & head , 9 ) ; push ( & head , 8 ) ; push ( & head , 15 ) ; push ( & head , 18 ) ; deleteEvenParityNodes ( & head ) ; printList ( head ) ; }