codeparrot/xlcost-text-to-code huggingface dataset:
Rotate a matrix by 90 degree in clockwise direction without using any extra space |  ; rotate function ; First rotation with respect to main diagonal ; Second rotation with respect to middle column ; to print matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE void rotate ( int arr [ N ] [ N ] ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < i ; ++ j ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N / 2 ; ++ j ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ i ] [ N - j - 1 ] ; arr [ i ] [ N - j - 1 ] = temp ; } } } void print ( int arr [ N ] [ N ] ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) cout << arr [ i ] [ j ] << " ▁ " ; cout << ' ' ; } } int main ( ) { int arr [ N ] [ N ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; print ( arr ) ; return 0 ; }
Rotate a matrix by 90 degree in clockwise direction without using any extra space |  ; rotate function ; First rotation with respect to Secondary diagonal ; Second rotation with respect to middle row ; To print matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE void rotate ( int arr [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N - i ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ N - 1 - j ] [ N - 1 - i ] ; arr [ N - 1 - j ] [ N - 1 - i ] = temp ; } } for ( int i = 0 ; i < N / 2 ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ N - 1 - i ] [ j ] ; arr [ N - 1 - i ] [ j ] = temp ; } } } void print ( int arr [ N ] [ N ] ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) cout << arr [ i ] [ j ] << " ▁ " ; cout << ' ' ; } } int main ( ) { int arr [ N ] [ N ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; print ( arr ) ; return 0 ; }
Elements that occurred only once in the array | C ++ implementation of above approach ; Function to find the elements that appeared only once in the array ; Sort the array ; Check for first element ; Check for all the elements if it is different its adjacent elements ; Check for the last element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void occurredOnce ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; if ( arr [ 0 ] != arr [ 1 ] ) cout << arr [ 0 ] << " ▁ " ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i - 1 ] ) cout << arr [ i ] << " ▁ " ; if ( arr [ n - 2 ] != arr [ n - 1 ] ) cout << arr [ n - 1 ] << " ▁ " ; } int main ( ) { int arr [ ] = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; occurredOnce ( arr , n ) ; return 0 ; }
Elements that occurred only once in the array | C ++ implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Store all the elements in the map with their occurrence ; Traverse the map and print all the elements with occurrence 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void occurredOnce ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; for ( auto it = mp . begin ( ) ; it != mp . end ( ) ; it ++ ) if ( it -> second == 1 ) cout << it -> first << " ▁ " ; } int main ( ) { int arr [ ] = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; occurredOnce ( arr , n ) ; return 0 ; }
Elements that occurred only once in the array | C ++ implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Check if the first and last element is equal If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void occurredOnce ( int arr [ ] , int n ) { int i = 1 , len = n ; if ( arr [ 0 ] == arr [ len - 1 ] ) { i = 2 ; len -- ; } for ( ; i < n ; i ++ ) if ( arr [ i ] == arr [ i - 1 ] ) i ++ ; else cout << arr [ i - 1 ] << " ▁ " ; if ( arr [ n - 1 ] != arr [ 0 ] && arr [ n - 1 ] != arr [ n - 2 ] ) cout << arr [ n - 1 ] ; } int main ( ) { int arr [ ] = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; occurredOnce ( arr , n ) ; return 0 ; }
Split the array and add the first part to the end | Set 2 | C ++ program to Split the array and add the first part to the end ; Function to reverse arr [ ] from index start to end ; Function to print an array ; Function to left rotate arr [ ] of size n by k ; Driver program to test above functions ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rvereseArray ( int arr [ ] , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; } void splitArr ( int arr [ ] , int k , int n ) { rvereseArray ( arr , 0 , n - 1 ) ; rvereseArray ( arr , 0 , n - k - 1 ) ; rvereseArray ( arr , n - k , n - 1 ) ; } int main ( ) { int arr [ ] = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; splitArr ( arr , k , n ) ; printArray ( arr , n ) ; return 0 ; }
Check if strings are rotations of each other or not | Set 2 | C ++ program to check if two strings are rotations of each other ; create lps [ ] that will hold the longest prefix suffix values for pattern ; length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; the loop calculates lps [ i ] for i = 1 to n - 1 ; Match from that rotating point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isRotation ( string a , string b ) { int n = a . length ( ) ; int m = b . length ( ) ; if ( n != m ) return false ; int lps [ n ] ; int len = 0 ; int i = 1 ; lps [ 0 ] = 0 ; while ( i < n ) { if ( a [ i ] == b [ len ] ) { lps [ i ] = ++ len ; ++ i ; } else { if ( len == 0 ) { lps [ i ] = 0 ; ++ i ; } else { len = lps [ len - 1 ] ; } } } i = 0 ; for ( int k = lps [ n - 1 ] ; k < m ; ++ k ) { if ( b [ k ] != a [ i ++ ] ) return false ; } return true ; } int main ( ) { string s1 = " ABACD " ; string s2 = " CDABA " ; cout << ( isRotation ( s1 , s2 ) ? "1" : "0" ) ; }
Count rotations divisible by 8 | C ++ program to count all rotations divisible by 8 ; function to count of all rotations divisible by 8 ; For single digit number ; For two - digit numbers ( considering all pairs ) ; first pair ; second pair ; considering all three - digit sequences ; Considering the number formed by the last digit and the first two digits ; Considering the number formed by the last two digits and the first digit ; required count of rotations ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRotationsDivBy8 ( string n ) { int len = n . length ( ) ; int count = 0 ; if ( len == 1 ) { int oneDigit = n [ 0 ] - '0' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ; int second = ( n [ 1 ] - '0' ) * 10 + ( n [ 0 ] - '0' ) ; if ( first % 8 == 0 ) count ++ ; if ( second % 8 == 0 ) count ++ ; return count ; } int threeDigit ; for ( int i = 0 ; i < ( len - 2 ) ; i ++ ) { threeDigit = ( n [ i ] - '0' ) * 100 + ( n [ i + 1 ] - '0' ) * 10 + ( n [ i + 2 ] - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; } threeDigit = ( n [ len - 1 ] - '0' ) * 100 + ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; threeDigit = ( n [ len - 2 ] - '0' ) * 100 + ( n [ len - 1 ] - '0' ) * 10 + ( n [ 0 ] - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; return count ; } int main ( ) { string n = "43262488612" ; cout << " Rotations : ▁ " << countRotationsDivBy8 ( n ) ; return 0 ; }
Minimum move to end operations to make all strings equal | CPP program to make all strings same using move to end operations . ; Returns minimum number of moves to end operations to make all strings same . ; Consider s [ i ] as target string and count rotations required to make all other strings same as str [ i ] . ; find function returns the index where we found arr [ i ] which is actually count of move - to - front operations . ; If any two strings are not rotations of each other , we can 't make them same.  ; driver code for above function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimunMoves ( string arr [ ] , int n ) { int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int curr_count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { string tmp = arr [ j ] + arr [ j ] ; int index = tmp . find ( arr [ i ] ) ; if ( index == string :: npos ) return -1 ; curr_count += index ; } ans = min ( curr_count , ans ) ; } return ans ; } int main ( ) { string arr [ ] = { " xzzwo " , " zwoxz " , " zzwox " , " xzzwo " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimunMoves ( arr , n ) ; return 0 ; }
Count rotations in sorted and rotated linked list | Program for count number of rotations in sorted linked list . ; Linked list node ; Function that count number of rotation in singly linked list . ; declare count variable and assign it 1. ; declare a min variable and assign to data of head node . ; check that while head not equal to NULL . ; if min value is greater then head -> data then it breaks the while loop and return the value of count . ; head assign the next value of head . ; Function to push element in linked list . ; Allocate dynamic memory for newNode . ; Assign the data into newNode . ; newNode -> next assign the address of head node . ; newNode become the headNode . ; Display linked list . ; Driver functions ; Create a node and initialize with NULL ; push ( ) insert node in linked list . 15 -> 18 -> 5 -> 8 -> 11 -> 12 ; Function call countRotation ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; int countRotation ( struct Node * head ) { int count = 0 ; int min = head -> data ; while ( head != NULL ) { if ( min > head -> data ) break ; count ++ ; head = head -> next ; } return count ; } void push ( struct Node * * head , int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> next = ( * head ) ; ( * head ) = newNode ; } void printList ( struct Node * node ) { while ( node != NULL ) { printf ( " % d ▁ " , node -> data ) ; node = node -> next ; } } int main ( ) { struct Node * head = NULL ; push ( & head , 12 ) ; push ( & head , 11 ) ; push ( & head , 8 ) ; push ( & head , 5 ) ; push ( & head , 18 ) ; push ( & head , 15 ) ; printList ( head ) ; cout << endl ; cout << " Linked ▁ list ▁ rotated ▁ elements : ▁ " ; cout << countRotation ( head ) << endl ; return 0 ; }
Rotate Linked List block wise | C ++ program to rotate a linked list block wise ; Link list node ; Recursive function to rotate one block ; Rotate Clockwise ; Rotate anti - Clockwise ; Function to rotate the linked list block wise ; If length is 0 or 1 return head ; if degree of rotation is 0 , return head ; Traverse upto last element of this block ; storing the first node of next block ; If nodes of this block are less than k . Rotate this block also ; Append the new head of next block to the tail of this block ; return head of updated Linked List ; Function to push a node ; Function to print linked list ; Driver code ; Start with the empty list ; create a list 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL ; k is block size and d is number of rotations in every block .
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; Node * rotateHelper ( Node * blockHead , Node * blockTail , int d , Node * * tail , int k ) { if ( d == 0 ) return blockHead ; if ( d > 0 ) { Node * temp = blockHead ; for ( int i = 1 ; temp -> next -> next && i < k - 1 ; i ++ ) temp = temp -> next ; blockTail -> next = blockHead ; * tail = temp ; return rotateHelper ( blockTail , temp , d - 1 , tail , k ) ; } if ( d < 0 ) { blockTail -> next = blockHead ; * tail = blockHead ; return rotateHelper ( blockHead -> next , blockHead , d + 1 , tail , k ) ; } } Node * rotateByBlocks ( Node * head , int k , int d ) { if ( ! head ! head -> next ) return head ; if ( d == 0 ) return head ; Node * temp = head , * tail = NULL ; int i ; for ( i = 1 ; temp -> next && i < k ; i ++ ) temp = temp -> next ; Node * nextBlock = temp -> next ; if ( i < k ) head = rotateHelper ( head , temp , d % k , & tail , i ) ; else head = rotateHelper ( head , temp , d % k , & tail , k ) ; tail -> next = rotateByBlocks ( nextBlock , k , d % k ) ; return head ; } void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( Node * node ) { while ( node != NULL ) { cout << node -> data << " ▁ " ; node = node -> next ; } } int main ( ) { Node * head = NULL ; for ( int i = 9 ; i > 0 ; i -= 1 ) push ( & head , i ) ; cout << " Given ▁ linked ▁ list ▁ STRNEWLINE " ; printList ( head ) ; int k = 3 , d = 2 ; head = rotateByBlocks ( head , k , d ) ; cout << " Rotated by blocks Linked list " ; printList ( head ) ; return ( 0 ) ; }
Check if two numbers are bit rotations of each other or not | C ++ program to check if two numbers are bit rotations of each other . ; function to check if two numbers are equal after bit rotation ; x64 has concatenation of x with itself . ; comapring only last 32 bits ; right shift by 1 unit ; driver code to test above function
#include <iostream> NEW_LINE using namespace std ; bool isRotation ( unsigned int x , unsigned int y ) { unsigned long long int x64 = x | ( ( unsigned long long int ) x << 32 ) ; while ( x64 >= y ) { if ( unsigned ( x64 ) == y ) return true ; x64 >>= 1 ; } return false ; } int main ( ) { unsigned int x = 122 ; unsigned int y = 2147483678 ; if ( isRotation ( x , y ) ) cout << " yes " << endl ; else cout << " no " << endl ; return 0 ; }
Count rotations divisible by 4 | C ++ program to count all rotation divisible by 4. ; Returns count of all rotations divisible by 4 ; For single digit number ; At - least 2 digit number ( considering all pairs ) ; Considering the number formed by the pair of last digit and 1 st digit ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRotations ( string n ) { int len = n . length ( ) ; if ( len == 1 ) { int oneDigit = n . at ( 0 ) - '0' ; if ( oneDigit % 4 == 0 ) return 1 ; return 0 ; } int twoDigit , count = 0 ; for ( int i = 0 ; i < ( len - 1 ) ; i ++ ) { twoDigit = ( n . at ( i ) - '0' ) * 10 + ( n . at ( i + 1 ) - '0' ) ; if ( twoDigit % 4 == 0 ) count ++ ; } twoDigit = ( n . at ( len - 1 ) - '0' ) * 10 + ( n . at ( 0 ) - '0' ) ; if ( twoDigit % 4 == 0 ) count ++ ; return count ; } int main ( ) { string n = "4834" ; cout << " Rotations : ▁ " << countRotations ( n ) << endl ; return 0 ; }
Check if a string can be obtained by rotating another string 2 places | C ++ program to check if a string is two time rotation of another string . ; Function to check if string2 is obtained by string 1 ; Initialize string as anti - clockwise rotation ; Initialize string as clock wise rotation ; check if any of them is equal to string1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isRotated ( string str1 , string str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false ; if ( str1 . length ( ) < 2 ) { return str1 . compare ( str2 ) == 0 ; } string clock_rot = " " ; string anticlock_rot = " " ; int len = str2 . length ( ) ; anticlock_rot = anticlock_rot + str2 . substr ( len - 2 , 2 ) + str2 . substr ( 0 , len - 2 ) ; clock_rot = clock_rot + str2 . substr ( 2 ) + str2 . substr ( 0 , 2 ) ; return ( str1 . compare ( clock_rot ) == 0 || str1 . compare ( anticlock_rot ) == 0 ) ; } int main ( ) { string str1 = " geeks " ; string str2 = " eksge " ; isRotated ( str1 , str2 ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Lexicographically minimum string rotation | Set 1 | A simple C ++ program to find lexicographically minimum rotation of a given string ; This functionr return lexicographically minimum rotation of str ; Find length of given string ; Create an array of strings to store all rotations ; Create a concatenation of string with itself ; One by one store all rotations of str in array . A rotation is obtained by getting a substring of concat ; Sort all rotations ; Return the first rotation from the sorted array ; Driver program to test above function
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; string minLexRotation ( string str ) { int n = str . length ( ) ; string arr [ n ] ; string concat = str + str ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = concat . substr ( i , n ) ; sort ( arr , arr + n ) ; return arr [ 0 ] ; } int main ( ) { cout << minLexRotation ( " GEEKSFORGEEKS " ) << endl ; cout << minLexRotation ( " GEEKSQUIZ " ) << endl ; cout << minLexRotation ( " BCABDADAB " ) << endl ; }
Next Greater Element in a Circular Linked List | C ++ program for the above approach ; Node structure of the circular Linked list ; Constructor ; Function to print the elements of a Linked list ; Iterate the linked list ; Print the data ; Function to find the next greater element for each node ; Stores the head of circular Linked list ; Stores the head of the resulting linked list ; Stores the temporary head of the resulting Linked list ; Iterate until head is not equal to H ; Used to iterate over the circular Linked List ; Stores if there exist any next Greater element ; Iterate until head is not equal to curr ; If the current node is smaller ; Update the value of Val ; Update curr ; If res is Null ; Create new Node with value curr -> data ; Assign address of res to tempList ; Update tempList ; Assign address of the next node to tempList ; Update the value of head node ; Print the resulting Linked list ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; Node ( int d ) { data = d ; next = NULL ; } } ; void print ( Node * head ) { Node * temp = head ; while ( temp != NULL ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } } void NextGreaterElement ( Node * head ) { Node * H = head ; Node * res = NULL ; Node * tempList = NULL ; do { Node * curr = head ; int Val = -1 ; do { if ( head -> data < curr -> data ) { Val = curr -> data ; break ; } curr = curr -> next ; } while ( curr != head ) ; if ( res == NULL ) { res = new Node ( Val ) ; tempList = res ; } else { tempList -> next = new Node ( Val ) ; tempList = tempList -> next ; } head = head -> next ; } while ( head != H ) ; print ( res ) ; } int main ( ) { Node * head = new Node ( 1 ) ; head -> next = new Node ( 5 ) ; head -> next -> next = new Node ( 12 ) ; head -> next -> next -> next = new Node ( 10 ) ; head -> next -> next -> next -> next = new Node ( 0 ) ; head -> next -> next -> next -> next -> next = head ; NextGreaterElement ( head ) ; return 0 ; }
Types of Linked List | Node of a doubly linked list
class Node { public : int data ; Node * next ; } ;
Types of Linked List | C ++ program to illustrate creation and traversal of Singly Linked List ; Structure of Node ; Function to print the content of linked list starting from the given node ; Iterate till n reaches NULL ; Print the data ; Driver Code ; Allocate 3 nodes in the heap ; Assign data in first node ; Link first node with second ; Assign data to second node ; Assign data to third node
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; void printList ( Node * n ) { while ( n != NULL ) { cout << n -> data << " ▁ " ; n = n -> next ; } } int main ( ) { Node * head = NULL ; Node * second = NULL ; Node * third = NULL ; head = new Node ( ) ; second = new Node ( ) ; third = new Node ( ) ; head -> data = 1 ; head -> next = second ; second -> data = 2 ; second -> next = third ; third -> data = 3 ; third -> next = NULL ; printList ( head ) ; return 0 ; }
Types of Linked List | Node of a doubly linked list
struct Node { int data ; struct Node * next ; struct Node * prev ; } ;
Types of Linked List | Structure for a node
class Node { public : int data ; Node * next ; } ;
Types of Linked List | Node of doubly circular linked list
struct Node { int data ; struct Node * next ; struct Node * prev ; } ;
Types of Linked List | Structure of the list
struct link { int info ; struct link * next ; } ;
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 ) ; }