Remove all even parity nodes from a Doubly and Circular Singly Linked List | Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; Create a new node and make head as next of it . ; If linked list is not None then set the next of last node ; Find the node before head and update next of it . ; Pofor the first node ; Function to deltete the node from a Circular Linked list ; If node to be delteted is head node ; Traverse list till not found deltete node ; Copy the address of the node ; Finally , free the memory occupied by delt ; Function that returns true if count of set bits in x is even ; parity will store the count of set bits ; Function to deltete all the Even Parity Nodes from the singly circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ has ▁ even ▁ parity , ▁ ▁ deltete ▁ node ▁ ' ptr ; Poto the next node ; Function to prnodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 11.9 .34 .6 .13 .21
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , data ) : NEW_LINE INDENT ptr1 = Node ( ) NEW_LINE temp = head_ref ; NEW_LINE ptr1 . data = data ; NEW_LINE ptr1 . next = head_ref ; NEW_LINE if ( head_ref != None ) : NEW_LINE INDENT while ( temp . next != head_ref ) : NEW_LINE INDENT temp = temp . next ; NEW_LINE DEDENT temp . next = ptr1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ptr1 . next = ptr1 ; NEW_LINE DEDENT head_ref = ptr1 ; NEW_LINE return head_ref NEW_LINE DEDENT def delteteNode ( head_ref , delt ) : NEW_LINE INDENT if ( head_ref == delt ) : NEW_LINE INDENT head_ref = delt . next ; NEW_LINE DEDENT temp = head_ref ; NEW_LINE while ( temp . next != delt ) : NEW_LINE INDENT temp = temp . next ; NEW_LINE DEDENT temp . next = delt . next ; NEW_LINE del ( delt ) ; 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 ) != 0 : 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 delteteEvenParityNodes ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return head ; NEW_LINE DEDENT if ( head == head . next ) : NEW_LINE INDENT if ( isEvenParity ( head . data ) ) : NEW_LINE INDENT head = None ; NEW_LINE DEDENT return head ; NEW_LINE DEDENT ptr = head ; NEW_LINE next = None NEW_LINE while True : NEW_LINE INDENT next = ptr . next ; NEW_LINE if ( isEvenParity ( ptr . data ) ) : NEW_LINE INDENT head = delteteNode ( head , ptr ) ; NEW_LINE DEDENT ptr = next ; NEW_LINE if ( ptr == head ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( head == head . next ) : NEW_LINE INDENT if ( isEvenParity ( head . data ) ) : NEW_LINE INDENT head = None ; NEW_LINE DEDENT return head ; NEW_LINE DEDENT return head ; NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT print ( " Empty ▁ List " ) NEW_LINE return ; NEW_LINE DEDENT temp = head ; NEW_LINE if ( head != None ) : NEW_LINE INDENT while True : NEW_LINE INDENT print ( temp . data , end = ' ▁ ' ) NEW_LINE temp = temp . next NEW_LINE if temp == head : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None ; NEW_LINE head = push ( head , 21 ) ; NEW_LINE head = push ( head , 13 ) ; NEW_LINE head = push ( head , 6 ) ; NEW_LINE head = push ( head , 34 ) ; NEW_LINE head = push ( head , 9 ) ; NEW_LINE head = push ( head , 11 ) ; NEW_LINE head = delteteEvenParityNodes ( head ) ; NEW_LINE printList ( head ) ; NEW_LINE DEDENT