codeparrot/xlcost-text-to-code huggingface dataset:
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
using System ; public class CircularLinkedList { public Node last ; public class Node { public int data ; public Node next ; } ; public Node addToEmpty ( int data ) { if ( this . last != null ) return this . last ; Node temp = new Node ( ) ; temp . data = data ; this . last = temp ; this . last . next = this . last ; return last ; } public Node addBegin ( int data ) { if ( last == null ) return addToEmpty ( data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = this . last . next ; this . last . next = temp ; return this . last ; } public void traverse ( ) { Node p ; if ( this . last == null ) { Console . WriteLine ( " List ▁ is ▁ empty . " ) ; return ; } p = this . last . next ; do { Console . Write ( p . data + " ▁ " ) ; p = p . next ; } while ( p != this . last . next ) ; Console . WriteLine ( " " ) ; } public int length ( ) { int x = 0 ; if ( this . last == null ) return x ; Node itr = this . last . next ; while ( itr . next != this . last . next ) { x ++ ; itr = itr . next ; } return ( x + 1 ) ; } public Node split ( int k ) { Node pass = new Node ( ) ; if ( this . last == null ) return this . last ; Node newLast , itr = this . last ; for ( int i = 0 ; i < k ; i ++ ) { itr = itr . next ; } newLast = itr ; pass . next = itr . next ; newLast . next = this . last . next ; this . last . next = pass . next ; return newLast ; } public static void Main ( String [ ] args ) { 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 ) ; Console . WriteLine ( " Original ▁ list : " ) ; clist . traverse ( ) ; int k = 4 ; CircularLinkedList clist2 = new CircularLinkedList ( ) ; clist2 . last = clist . split ( k ) ; Console . WriteLine ( " The ▁ new ▁ lists ▁ are : " ) ; clist2 . traverse ( ) ; clist . traverse ( ) ; } }
Convert a given Binary Tree to Circular Doubly Linked List | Set 2 | 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
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left ; public Node right ; public Node ( int x ) { data = x ; left = right = null ; } } ; static void inorder ( Node root , List < int > v ) { if ( root == null ) return ; inorder ( root . left , v ) ; v . Add ( root . data ) ; inorder ( root . right , v ) ; } static Node bTreeToCList ( Node root ) { if ( root == null ) return null ; List < int > v = new List < int > ( ) ; inorder ( root , v ) ; Node head_ref = new Node ( v [ 0 ] ) ; Node curr = head_ref ; for ( int i = 1 ; i < v . Count ; 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 ; } static void displayCList ( Node head ) { Console . WriteLine ( " Circular ▁ Doubly ▁ " + " Linked ▁ List ▁ is ▁ : " ) ; Node itr = head ; do { Console . Write ( itr . data + " ▁ " ) ; itr = itr . right ; } while ( head != itr ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { 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 ) ; } }
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
static Node DeleteAllEvenNode ( Node head ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { Console . Write ( " List is " return null ; } if ( len < 2 ) { return null ; } previous = head ; next = previous . next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous . next = next . next ; previous = next . next ; next = previous . next ; } len -- ; count ++ ; } return head ; }
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
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static void printMinMax ( Node head ) { if ( head == null ) { return ; } Node current ; current = head ; int min = int . MaxValue , max = int . MinValue ; while ( current . next != head ) { if ( current . data < min ) { min = current . data ; } if ( current . data > max ) { max = current . data ; } current = current . next ; } Console . WriteLine ( " Minimum = " ▁ + ▁ min ▁ + ▁ " , Maximum = " } static Node ( Node head , int data ) { current = head ; newNode = new Node ( ) ; if ( newNode == null ) { Console . Write ( " STRNEWLINE Memory ▁ Error STRNEWLINE " ) ; return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; = newNode ; return ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; . next = newNode ; } return ; } static void ( Node head ) { current = head ; if ( head == null ) { Console . Write ( " STRNEWLINE Display ▁ List ▁ is ▁ empty STRNEWLINE " ) ; return ; } else { do { Console . Write ( " { 0 } ▁ " , current . data ) ; = current . next ; } while ( current != head ) ; } } public static void ( ) { Head = null ; Head = insertNode ( Head , 99 ) ; Head = insertNode ( Head , 11 ) ; Head = insertNode ( Head , 22 ) ; Head = insertNode ( Head , 33 ) ; Head = insertNode ( Head , 44 ) ; Head = insertNode ( Head , 55 ) ; Head = insertNode ( Head , 66 ) ; Console . WriteLine ( " Initial ▁ List : ▁ " ) ; displayList ( Head ) ; printMinMax ( Head ) ; } }
Delete all the even nodes of a Circular Linked List | C # program to delete all prime 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 ; 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
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; 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 ; return head_ref ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node deleteEvenNodes ( Node head ) { Node ptr = head ; Node next ; do { if ( ptr . data % 2 == 0 ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; return head ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 61 ) ; head = push ( head , 12 ) ; head = push ( head , 56 ) ; head = push ( head , 2 ) ; head = push ( head , 11 ) ; head = push ( head , 57 ) ; Console . WriteLine ( " STRNEWLINE List ▁ after ▁ deletion ▁ : ▁ " ) ; head = deleteEvenNodes ( head ) ; printList ( head ) ; } }
Sum of the nodes of a Circular Linked List | C # 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
using System ; class GFG { class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; 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 ; return head_ref ; } static int sumOfList ( Node head ) { Node temp = head ; int sum = 0 ; if ( head != null ) { do { temp = temp . next ; sum += temp . data ; } while ( temp != head ) ; } return sum ; } public static void Main ( ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 56 ) ; head = push ( head , 2 ) ; head = push ( head , 11 ) ; Console . WriteLine ( " Sum ▁ of ▁ Circular ▁ linked " + " ▁ list ▁ is ▁ = ▁ " + sumOfList ( head ) ) ; } }
Delete every Kth node from circular linked list | C # program to delete every kth Node from circular linked list . ; structure for a Node ; Utility function to print the circular linked list ; Function to delete every kth Node ; If list is empty , simply return . ; take two pointers - current and previous ; Check if Node is the only Node \ If yes , we reached the goal , therefore return . ; Print intermediate list . ; If more than one Node present in the list , Make previous pointer point to current Iterate current pointer k times , i . e . current Node is to be deleted . ; If Node to be deleted is head ; If Node to be deleted is last Node . ; Function to insert a Node at the end of a Circular linked list ; Create a new Node ; if the list is empty , make the new Node head Also , it will point to itself . ; traverse the list to reach the last Node and insert the Node ; Driver code ; insert Nodes in the circular linked list ; Delete every kth Node from the circular linked list .
using System ; class GFG { public class Node { public int data ; public Node next ; public Node ( int x ) { data = x ; next = null ; } } ; static void printList ( Node head ) { if ( head == null ) return ; Node temp = head ; do { Console . Write ( temp . data + " - > " ) ; temp = temp . next ; } while ( temp != head ) ; Console . WriteLine ( head . data ) ; } static Node deleteK ( Node head_ref , int k ) { Node head = head_ref ; if ( head == null ) return null ; Node curr = head , prev = null ; while ( true ) { if ( curr . next == head && curr == head ) break ; printList ( head ) ; for ( int i = 0 ; i < k ; i ++ ) { prev = curr ; curr = curr . next ; } if ( curr == head ) { prev = head ; while ( prev . next != head ) prev = prev . next ; head = curr . next ; prev . next = head ; head_ref = head ; } else if ( curr . next == head ) { prev . next = head ; } else { prev . next = curr . next ; } } return head ; } static Node insertNode ( Node head_ref , int x ) { Node head = head_ref ; Node temp = new Node ( x ) ; if ( head == null ) { temp . next = temp ; head_ref = temp ; return head_ref ; } else { Node temp1 = head ; while ( temp1 . next != head ) temp1 = temp1 . next ; temp1 . next = temp ; temp . next = head ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = insertNode ( head , 1 ) ; head = insertNode ( head , 2 ) ; head = insertNode ( head , 3 ) ; head = insertNode ( head , 4 ) ; head = insertNode ( head , 5 ) ; head = insertNode ( head , 6 ) ; head = insertNode ( head , 7 ) ; head = insertNode ( head , 8 ) ; head = insertNode ( head , 9 ) ; int k = 4 ; head = deleteK ( head , k ) ; } }
Convert an Array to a Circular Doubly Linked List | C # program to convert array to circular doubly linked list ; Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array to be converted ; Start Pointer ; Create the List ; Display the list
using System ; class GFG { public class node { public int data ; public node next ; public node prev ; } ; static node getNode ( ) { return new node ( ) ; } static int displayList ( node temp ) { node t = temp ; if ( temp == null ) return 0 ; else { Console . Write ( " The ▁ list ▁ is : ▁ " ) ; while ( temp . next != t ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } Console . Write ( temp . data ) ; return 1 ; } } static node createList ( int [ ] arr , int n , node start ) { node newNode , temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = ( start ) . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; node start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; } }
Lucky alive person in a circle | Code Solution to sword puzzle | C # code to find the luckiest person ; Node structure ; Function to find the luckiest person ; Create a single node circular linked list . ; Starting from first soldier . ; condition for evaluating the existence of single soldier who is not killed . ; deleting soldier from the circular list who is killed in the fight . ; Returning the Luckiest soldier who remains alive . ; Driver code
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . next = null ; return node ; } static int alivesol ( int Num ) { if ( Num == 1 ) return 1 ; Node last = newNode ( 1 ) ; last . next = last ; for ( int i = 2 ; i <= Num ; i ++ ) { Node tem = newNode ( i ) ; tem . next = last . next ; last . next = tem ; last = tem ; } Node curr = last . next ; Node tem1 = new Node ( ) ; while ( curr . next != curr ) { tem1 = curr ; curr = curr . next ; tem1 . next = curr . next ; tem1 = tem1 . next ; curr = tem1 ; } int res = tem1 . data ; return res ; } public static void Main ( String [ ] args ) { int N = 100 ; Console . WriteLine ( alivesol ( N ) ) ; } }
Lowest Common Ancestor of the deepest leaves of a Binary Tree | C # program for the above approach ; Node of a Binary Tree ; Function to find the depth of the Binary Tree ; If root is not null ; Left recursive subtree ; Right recursive subtree ; Returns the maximum depth ; Function to perform the depth first search on the binary tree ; If root is null ; If curr is equal to depth ; Left recursive subtree ; Right recursive subtree ; If left and right are not null ; Return left , if left is not null Otherwise return right ; Function to find the LCA of the deepest nodes of the binary tree ; If root is null ; Stores the deepest depth of the binary tree ; Return the LCA of the nodes at level depth ; Driver code ; Given Binary Tree
using System ; class Node { public Node left = null ; public Node right = null ; public int data ; public Node ( int data ) { this . data = data ; } } public class GFG { static int findDepth ( Node root ) { if ( root == null ) return 0 ; int left = findDepth ( root . left ) ; int right = findDepth ( root . right ) ; return 1 + Math . Max ( left , right ) ; } static Node DFS ( Node root , int curr , int depth ) { if ( root == null ) return null ; if ( curr == depth ) return root ; Node left = DFS ( root . left , curr + 1 , depth ) ; Node right = DFS ( root . right , curr + 1 , depth ) ; if ( left != null && right != null ) return root ; return ( left != null ) ? left : right ; } static Node lcaOfDeepestLeaves ( Node root ) { if ( root == null ) return null ; int depth = findDepth ( root ) - 1 ; return DFS ( root , 0 , depth ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . right . left . left = new Node ( 8 ) ; root . right . left . right = new Node ( 9 ) ; Console . WriteLine ( lcaOfDeepestLeaves ( root ) . data ) ; } }
Check if two nodes are on same path in a tree | Set 2 | C # program to check if two nodes are on same path in a tree without using any extra space ; Function to filter the return Values ; Utility function to check if nodes are on same path or not ; Condition to check if any vertex is equal to given two vertex or not ; Check if the current position has 1 ; Recursive call ; Return LCA ; Function to check if nodes lies on same path or not ; Driver code
using System ; class GFG { static int filter ( int x , int y , int z ) { if ( x != - 1 && y != - 1 ) { return z ; } return x == - 1 ? y : x ; } static int samePathUtil ( int [ , ] mtrx , int vrtx , int v1 , int v2 , int i ) { int ans = - 1 ; if ( i == v1 i == v2 ) return i ; for ( int j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i , j ] == 1 ) { ans = filter ( ans , samePathUtil ( mtrx , vrtx , v1 , v2 , j ) , i ) ; } } return ans ; } static bool isVertexAtSamePath ( int [ , ] mtrx , int vrtx , int v1 , int v2 , int i ) { int lca = samePathUtil ( mtrx , vrtx , v1 - 1 , v2 - 1 , i ) ; if ( lca == v1 - 1 lca == v2 - 1 ) return true ; return false ; } public static void Main ( String [ ] args ) { int vrtx = 7 ; int [ , ] mtrx = { { 0 , 1 , 1 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } } ; int v1 = 1 , v2 = 5 ; if ( isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , 0 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }