Remove all even parity nodes from a Doubly and Circular Singly 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 None ; 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 . delt - . 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 delt ; 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
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data NEW_LINE new_node . prev = None NEW_LINE new_node . next = ( head_ref ) NEW_LINE if ( ( head_ref ) != None ) : NEW_LINE INDENT ( head_ref ) . prev = new_node NEW_LINE DEDENT ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def isEvenParity ( x ) : NEW_LINE INDENT parity = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT parity += 1 NEW_LINE DEDENT x = x >> 1 NEW_LINE DEDENT if ( parity % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def deleteNode ( head_ref , delt ) : NEW_LINE INDENT if ( head_ref == None or delt == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( head_ref == delt ) : NEW_LINE INDENT head_ref = delt . next NEW_LINE DEDENT if ( delt . next != None ) : NEW_LINE INDENT delt . next . prev = delt . prev NEW_LINE DEDENT if ( delt . prev != None ) : NEW_LINE INDENT delt . prev . next = delt . next NEW_LINE DEDENT del ( delt ) NEW_LINE return head_ref NEW_LINE DEDENT def deleteEvenParityNodes ( head_ref ) : NEW_LINE INDENT ptr = head_ref NEW_LINE next = None NEW_LINE while ( ptr != None ) : NEW_LINE INDENT next = ptr . next NEW_LINE if ( isEvenParity ( ptr . data ) ) : NEW_LINE INDENT head_ref = deleteNode ( head_ref , ptr ) NEW_LINE DEDENT ptr = next NEW_LINE DEDENT return head_ref NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT print ( " Empty list " ) NEW_LINE return NEW_LINE DEDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = ' ▁ ' ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 14 ) NEW_LINE head = push ( head , 9 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 15 ) NEW_LINE head = push ( head , 18 ) NEW_LINE head = deleteEvenParityNodes ( head ) NEW_LINE printList ( head ) NEW_LINE DEDENT