codeparrot/xlcost-text-to-code huggingface dataset:
Remove all even parity nodes from a Doubly and Circular Singly Linked List | C ++ program to remove all the Even Parity Nodes from a 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 NULL then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Function to delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by del ; Function that returns true if count of set bits in x is even ; parity will store the count of set bits ; Function to delete 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 , ▁ ▁ delete ▁ node ▁ ' ptr ' ; Point to the next node ; Function to print nodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 11 -> 9 -> 34 -> 6 -> 13 -> 21
#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 ) { if ( head_ref == del ) head_ref = del -> next ; struct Node * temp = head_ref ; while ( temp -> next != del ) { temp = temp -> next ; } temp -> next = del -> next ; free ( del ) ; return ; } 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 deleteEvenParityNodes ( Node * & head ) { if ( head == NULL ) return ; if ( head == head -> next ) { if ( isEvenParity ( head -> data ) ) head = NULL ; return ; } struct Node * ptr = head ; struct Node * next ; do { next = ptr -> next ; if ( isEvenParity ( ptr -> data ) ) deleteNode ( head , ptr ) ; ptr = next ; } while ( ptr != head ) ; if ( head == head -> next ) { if ( isEvenParity ( head -> data ) ) head = NULL ; return ; } } void printList ( struct Node * head ) { if ( head == NULL ) { cout << " Empty ▁ List STRNEWLINE " ; return ; } 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 , 21 ) ; push ( & head , 13 ) ; push ( & head , 6 ) ; push ( & head , 34 ) ; push ( & head , 9 ) ; push ( & head , 11 ) ; deleteEvenParityNodes ( head ) ; printList ( head ) ; return 0 ; }
Remove all the Even Digit Sum Nodes from a Circular Singly Linked List | C ++ program to remove all the Even Digit Sum Nodes from a 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 NULL then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Function to delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by del ; Function to find the digit sum for a number ; Function to delete all the Even Digit Sum Nodes from the singly circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ is ▁ Fibonacci , ▁ ▁ delete ▁ node ▁ ' ptr ' ; Point to the next node ; Function to print nodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 9 -> 11 -> 34 -> 6 -> 13 -> 21
#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 ; } int digitSum ( int num ) { int sum = 0 ; while ( num ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } void deleteEvenDigitSumNodes ( Node * head ) { struct Node * ptr = head ; struct Node * next ; do { if ( ! ( digitSum ( ptr -> data ) & 1 ) ) 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 , 21 ) ; push ( & head , 13 ) ; push ( & head , 6 ) ; push ( & head , 34 ) ; push ( & head , 11 ) ; push ( & head , 9 ) ; deleteEvenDigitSumNodes ( head ) ; printList ( head ) ; return 0 ; }
Delete all odd nodes of a Circular Linked List | C ++ program to delete all odd 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 odd ; 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 odd nodes from the singly circular linked list ; Traverse list till the end if the node is odd then delete it ; if node is odd ; point to next node ; Function to print nodes ; Driver code ; Initialize lists as empty ; Created linked list will be 56 -> 61 -> 57 -> 11 -> 12 -> 2
#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 deleteoddNodes ( Node * head ) { struct Node * ptr = head ; struct Node * next ; do { if ( ( ptr -> data % 2 ) == 1 ) 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 , 2 ) ; push ( & head , 12 ) ; push ( & head , 11 ) ; push ( & head , 57 ) ; push ( & head , 61 ) ; push ( & head , 56 ) ; cout << " List after deletion : " ; deleteoddNodes ( head ) ; printList ( head ) ; return 0 ; }
Insertion in a sorted circular linked list when a random pointer is given | C ++ implementation of the approach ; Node structure ; Function to create a node ; Function to find and return the head ; If the list is empty ; Finding the last node of the linked list the last node must have the highest value if no such element is present then all the nodes of the linked list must be same ; Return the head ; Function to insert a new_node in the list in sorted fashion Note that this function expects a pointer to head node as this can modify the head of the input linked list ; If the list is empty ; If the node to be inserted is the smallest then it has to be the new head ; Find the last node of the list as it will be pointing to the head ; Locate the node before the point of insertion ; Return the new head ; Function to print the nodes of the linked list ; Driver code ; Start with an empty linked list ; Create linked list from the given array ; Move to a random node if it exists ; Print the contents of the created list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { Node * next ; int data ; } ; Node * create ( ) { Node * new_node = ( Node * ) malloc ( sizeof ( Node ) ) ; new_node -> next = NULL ; return new_node ; } Node * find_head ( Node * random ) { if ( random == NULL ) return NULL ; Node * head , * var = random ; while ( ! ( var -> data > var -> next -> data var -> next == random ) ) { var = var -> next ; } return var -> next ; } Node * sortedInsert ( Node * head_ref , Node * new_node ) { Node * current = head_ref ; if ( current == NULL ) { new_node -> next = new_node ; head_ref = new_node ; } else if ( current -> data >= new_node -> data ) { while ( current -> next != head_ref ) current = current -> next ; current -> next = new_node ; new_node -> next = head_ref ; head_ref = new_node ; } else { while ( current -> next != head_ref && current -> next -> data < new_node -> data ) { current = current -> next ; } new_node -> next = current -> next ; current -> next = new_node ; } return head_ref ; } void printList ( Node * start ) { Node * temp ; if ( start != NULL ) { temp = start ; do { cout << temp -> data << " ▁ " ; temp = temp -> next ; } while ( temp != start ) ; } } int main ( ) { int arr [ ] = { 12 , 56 , 2 , 11 , 1 , 90 } ; int list_size , i ; Node * start = NULL ; Node * temp ; for ( i = 0 ; i < 6 ; i ++ ) { if ( start != NULL ) for ( int j = 0 ; j < ( rand ( ) % 10 ) ; j ++ ) start = start -> next ; temp = create ( ) ; temp -> data = arr [ i ] ; start = sortedInsert ( find_head ( start ) , temp ) ; } printList ( find_head ( start ) ) ; return 0 ; }
Splitting starting N nodes into new Circular Linked List while preserving the old nodes | C ++ implementation of the approach ; Function to add a node to the empty list ; If not empty ; Creating a node dynamically ; Assigning the data ; Creating the link ; Function to add a node to the beginning of the list ; If list is empty ; Create node ; Assign data ; Function to traverse and print the list ; If list is empty ; Pointing to the first Node of the list ; Traversing the list ; Function to find the length of the CircularLinkedList ; Stores the length ; List is empty ; Iterator Node to traverse the List ; Return the length of the list ; Function to split the first k nodes into a new CircularLinkedList and the remaining nodes stay in the original CircularLinkedList ; Empty Node for reference ; Check if the list is empty If yes , then return NULL ; NewLast will contain the last node of the new split list itr to iterate the node till the required node ; Update NewLast to the required node and link the last to the start of rest of the list ; Return the last node of the required list ; Driver code ; Create a new list for the starting k nodes ; Append the new last node into the new list ; Print the new lists
#include <bits/stdc++.h> NEW_LINE using namespace std ; class CircularLinkedList { public : struct Node { int data ; Node * next ; } ; Node * last ; Node * addToEmpty ( int data ) { if ( last != NULL ) return last ; Node * temp = new Node ( ) ; temp -> data = data ; last = temp ; last -> next = last ; return last ; } Node * addBegin ( int data ) { if ( last == NULL ) return addToEmpty ( data ) ; Node * temp = new Node ( ) ; temp -> data = data ; temp -> next = last -> next ; last -> next = temp ; return last ; } void traverse ( ) { Node * p ; if ( last == NULL ) { cout << ( " List ▁ is ▁ empty . " ) ; return ; } p = last -> next ; do { cout << p -> data << " ▁ " ; p = p -> next ; } while ( p != last -> next ) ; cout << endl ; } int length ( ) { int x = 0 ; if ( last == NULL ) return x ; Node * itr = last -> next ; while ( itr -> next != last -> next ) { x ++ ; itr = itr -> next ; } return ( x + 1 ) ; } Node * split ( int k ) { Node * pass = new Node ( ) ; if ( last == NULL ) return last ; Node * newLast , * itr = last ; for ( int i = 0 ; i < k ; i ++ ) { itr = itr -> next ; } newLast = itr ; pass -> next = itr -> next ; newLast -> next = last -> next ; last -> next = pass -> next ; return newLast ; } } ; int main ( ) { CircularLinkedList * clist = new CircularLinkedList ( ) ; clist -> last = NULL ; clist -> addToEmpty ( 12 ) ; clist -> addBegin ( 10 ) ; clist -> addBegin ( 8 ) ; clist -> addBegin ( 6 ) ; clist -> addBegin ( 4 ) ; clist -> addBegin ( 2 ) ; cout << ( " Original ▁ list : " ) ; clist -> traverse ( ) ; int k = 4 ; CircularLinkedList * clist2 = new CircularLinkedList ( ) ; clist2 -> last = clist -> split ( k ) ; cout << ( " The ▁ new ▁ lists ▁ are : " ) ; clist2 -> traverse ( ) ; clist -> traverse ( ) ; }
Convert a given Binary Tree to Circular Doubly Linked List | Set 2 | A C ++ program for conversion of Binary Tree to CDLL ; A binary tree node has data , and left and right pointers ; Function to perform In - Order traversal of the tree and store the nodes in a vector ; first recur on left child ; append the data of node in vector ; now recur on right child ; Function to convert Binary Tree to Circular Doubly Linked list using the vector which stores In - Order traversal of the Binary Tree ; Base cases ; Vector to be used for storing the nodes of tree in In - order form ; Calling the In - Order traversal function ; Create the head of the linked list pointing to the root of the tree ; Create a current pointer to be used in traversal ; Traversing the nodes of the tree starting from the second elements ; Create a temporary pointer pointing to current ; Current 's right points to the current  node in traversal ; Current points to its right ; Current 's left points to temp ; Current 's right points to head of the list ; Head 's left points to current ; Return head of the list ; Display Circular Link List ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; Node ( int x ) { data = x ; left = right = NULL ; } } ; void inorder ( Node * root , vector < int > & v ) { if ( ! root ) return ; inorder ( root -> left , v ) ; v . push_back ( root -> data ) ; inorder ( root -> right , v ) ; } Node * bTreeToCList ( Node * root ) { if ( root == NULL ) return NULL ; vector < int > v ; inorder ( root , v ) ; Node * head_ref = new Node ( v [ 0 ] ) ; Node * curr = head_ref ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { Node * temp = curr ; curr -> right = new Node ( v [ i ] ) ; curr = curr -> right ; curr -> left = temp ; } curr -> right = head_ref ; head_ref -> left = curr ; return head_ref ; } void displayCList ( Node * head ) { cout << " Circular ▁ Doubly ▁ Linked ▁ List ▁ is ▁ : STRNEWLINE " ; Node * itr = head ; do { cout << itr -> data << " ▁ " ; itr = itr -> right ; } while ( head != itr ) ; cout << " STRNEWLINE " ; } int main ( ) { Node * root = new Node ( 10 ) ; root -> left = new Node ( 12 ) ; root -> right = new Node ( 15 ) ; root -> left -> left = new Node ( 25 ) ; root -> left -> right = new Node ( 30 ) ; root -> right -> left = new Node ( 36 ) ; Node * head = bTreeToCList ( root ) ; displayCList ( head ) ; return 0 ; }
Delete all odd or even positioned nodes from Circular Linked List | Function to delete that all node whose index position is odd ; check list have any node if not then return ; if list have single node means odd position then delete it ; traverse first to last if list have more than one node ; delete first position node which is odd position ; Function to delete first node ; check position is odd or not if yes then delete node
void DeleteAllOddNode ( struct Node * * head ) { int len = Length ( * head ) ; int count = 0 ; struct Node * previous = * head , * next = * head ; if ( * head == NULL ) { printf ( " Delete Last List is empty " return ; } if ( len == 1 ) { DeleteFirst ( head ) ; return ; } while ( len > 0 ) { if ( count == 0 ) { DeleteFirst ( head ) ; } if ( count % 2 == 0 && count != 0 ) { deleteNode ( * head , previous ) ; } previous = previous -> next ; next = previous -> next ; len -- ; count ++ ; } return ; }
Delete all odd or even positioned nodes from Circular Linked List | Function to delete all even position nodes ; Take size of list ; Check list is empty if empty simply return ; if list have single node then return ; make first node is previous ; make second node is current ; check node number is even if node is even then delete that node
void DeleteAllEvenNode ( struct Node * * head ) { int len = Length ( * head ) ; int count = 1 ; struct Node * previous = * head , * next = * head ; if ( * head == NULL ) { printf ( " List is empty " return ; } if ( len < 2 ) { return ; } previous = * head ; next = previous -> next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous -> next = next -> next ; free ( next ) ; previous = next -> next ; next = previous -> next ; } len -- ; count ++ ; } return ; }
Find minimum and maximum elements in singly Circular Linked List | C ++ program to find minimum and maximum value from singly circular linked list ; structure for a node ; Function to print minimum and maximum nodes of the circular linked list ; check list is empty ; pointer for traversing ; initialize head to current pointer ; initialize max int value to min initialize min int value to max ; While last node is not reached ; If current node data is lesser for min then replace it ; If current node data is greater for max then replace it ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function to print the Circular linked list ; if list is empty simply show message ; traverse first to last node ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void printMinMax ( struct Node * * head ) { if ( * head == NULL ) { return ; } struct Node * current ; current = * head ; int min = INT_MAX , max = INT_MIN ; do { if ( current -> data < min ) { min = current -> data ; } if ( current -> data > max ) { max = current -> data ; } current = current -> next ; } while ( current != head ) ; cout << " Minimum = " ▁ < < ▁ min ▁ < < ▁ " , Maximum = " } void insertNode ( struct Node * * head , int data ) { struct Node * current = * head ; struct Node * newNode = new Node ; if ( ! newNode ) { printf ( " Memory Error " return ; } newNode -> data = data ; if ( * head == NULL ) { newNode -> next = newNode ; * head = newNode ; return ; } else { while ( current -> next != * head ) { current = current -> next ; } newNode -> next = * head ; current -> next = newNode ; } } void displayList ( struct Node * head ) { struct Node * current = head ; if ( head == NULL ) { printf ( " Display List is empty " return ; } else { do { printf ( " % d ▁ " , current -> data ) ; current = current -> next ; } while ( current != head ) ; } } int main ( ) { struct Node * Head = NULL ; insertNode ( & Head , 99 ) ; insertNode ( & Head , 11 ) ; insertNode ( & Head , 22 ) ; insertNode ( & Head , 33 ) ; insertNode ( & Head , 44 ) ; insertNode ( & Head , 55 ) ; insertNode ( & Head , 66 ) ; cout << " Initial ▁ List : ▁ " ; displayList ( Head ) ; printMinMax ( & Head ) ; return 0 ; }
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 ; }
Sum of the nodes of a Circular Linked List | CPP program to find the sum of all nodes of a Circular 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 ; Function to find sum of the given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 11 -> 2 -> 56 -> 12
#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 ; } int sumOfList ( struct Node * head ) { struct Node * temp = head ; int sum = 0 ; if ( head != NULL ) { do { temp = temp -> next ; sum += temp -> data ; } while ( temp != head ) ; } return sum ; } int main ( ) { struct Node * head = NULL ; push ( & head , 12 ) ; push ( & head , 56 ) ; push ( & head , 2 ) ; push ( & head , 11 ) ; cout << " Sum ▁ of ▁ Circular ▁ linked ▁ list ▁ is ▁ = ▁ " << sumOfList ( head ) ; return 0 ; }