codeparrot/xlcost-text-to-code huggingface dataset:
Josephus Circle implementation using STL list | Node class to store data ; * Function to find the only person left after one in every m - th node is killed * in a circle of n nodes ; Create a circular linked list of size N . ; Connect last node to first ; * while only one node is left in the linked list ; Find m - th node ; Remove the m - th node ; Driver program to test above functions
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function getJosephusPosition ( m , n ) { var head = new Node ( 1 ) ; var prev = head ; for ( i = 2 ; i <= n ; i ++ ) { prev . next = new Node ( i ) ; prev = prev . next ; } prev . next = head ; var ptr1 = head , ptr2 = head ; while ( ptr1 . next != ptr1 ) { var count = 1 ; while ( count != m ) { ptr2 = ptr1 ; ptr1 = ptr1 . next ; count ++ ; } ptr2 . next = ptr1 . next ; ptr1 = ptr2 . next ; } document . write ( " " + " " + ptr1 . data ) ; } var n = 14 , m = 2 ; getJosephusPosition ( m , n ) ;
Swap Kth node from beginning with Kth node from end in a Linked List | A Linked List node ; * Utility function to insert a node at the beginning ; Utility function for displaying linked list ; * Utility function for calculating length of linked list ; * Function for swapping kth nodes from both ends of linked list ; Count nodes in linked list ; Check if k is valid ; If x ( kth node from start ) and y ( kth node from end ) are same ; Find the kth node from beginning of linked list . We also find previous of kth node because we need to update next pointer of the previous . ; Similarly , find the kth node from end and its previous . kth node from end is ( n - k + 1 ) th node from beginning ; If x_prev exists , then new next of it will be y . Consider the case when y -> next is x , in this case , x_prev and y are same . So the statement " x _ prev - > next ▁ = ▁ y " creates a self loop . This self loop will be broken when we change y -> next . ; Same thing applies to y_prev ; Swap next pointers of x and y . These statements also break self loop if x -> next is y or y -> next is x ; Change head pointers when k is 1 or n ; Driver code to test above
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } var head ; function push ( new_data ) { new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } function printList ( ) { node = head ; while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } document . write ( " " ) ; } function countNodes ( ) { var count = 0 ; s = head ; while ( s != null ) { count ++ ; s = s . next ; } return count ; } function swapKth ( k ) { var n = countNodes ( ) ; if ( n < k ) return ; if ( 2 * k - 1 == n ) return ; x = head ; x_prev = null ; for ( i = 1 ; i < k ; i ++ ) { x_prev = x ; x = x . next ; } y = head ; y_prev = null ; for ( i = 1 ; i < n - k + 1 ; i ++ ) { y_prev = y ; y = y . next ; } if ( x_prev != null ) x_prev . next = y ; if ( y_prev != null ) y_prev . next = x ; temp = x . next ; x . next = y . next ; y . next = temp ; if ( k == 1 ) head = y ; if ( k == n ) head = x ; } for ( let i = 8 ; i >= 1 ; i -- ) push ( i ) ; document . write ( " " ) ; printList ( ) ; document . write ( " " ) ; for ( let i = 1 ; i < 9 ; i ++ ) { swapKth ( i ) ; document . write ( " " + i + " " ) ; printList ( ) ; document . write ( " " ) ; }
Count triplets in a sorted doubly linked list whose sum is equal to a given value x | Structure of node of doubly linked list ; Function to count pairs whose sum equal to given ' value ' ; The loop terminates when either of two pointers become null , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; Pair found ; Increment count ; Move first in forward direction ; Move second in backward direction ; If sum is greater than ' value ' move second in backward direction ; Else move first in forward direction ; Required count of pairs ; Function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; If list is empty ; Get pointer to the last node of the doubly linked list ; Traversing the doubly linked list ; For each current node ; Count pairs with sum ( x - current . data ) in the range first to last and add it to the ' count ' of triplets ; Required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; Allocate node ; Put in the data ; Start with an empty doubly linked list ; Insert values in sorted order
class Node { constructor ( data ) { this . data = data ; this . next = this . prev = null ; } } function countPairs ( first , second , value ) { let count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data + second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data + second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } function countTriplets ( head , x ) { if ( head == null ) return 0 ; let current , first , last ; let count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x - current . data ) ; } return count ; } function insert ( head , data ) { let temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } let head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; let x = 17 ; document . write ( " " + countTriplets ( head , x ) ) ;
Remove duplicates from an unsorted doubly linked list | javascript implementation to remove duplicates a node of the doubly linked list from an unsorted doubly linked list ; 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 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 ; function to remove duplicates from an unsorted doubly linked list ; if DLL is empty or if it contains only a single node ; pick elements one by one ; Compare the picked element with the rest of the elements ; if duplicate , then delete it ; store pointer to the node next to ' ptr2' ; delete node pointed to by ' ptr2' ; update ' ptr2' ; else simply move to the next node ; Function to insert a node at the beginning of the Doubly Linked List ; allocate node ; put in the data ; since we are adding at the beginning , prev is always null ; link the old list off the new node ; change prev of head node to new node ; move the head to povar to the new node ; Function to prvar nodes in a given doubly linked list ; if list is empty ; Driver Code ; Create the doubly linked list : 8 < .4 < .4 < .6 < .4 < .8 < .4 < .10 < .12 < .12 ; remove duplicate nodes
class Node { constructor ( ) { this . data = 0 ; this . prev = null ; this . next = null ; } } function deleteNode ( head_ref , del ) { if ( head_ref == null del == null ) return head_ref ; 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 ; return head_ref ; } function removeDuplicates ( head_ref ) { if ( ( head_ref ) == null || ( head_ref ) . next == null ) return head_ref ; var ptr1 , ptr2 ; for ( ptr1 = head_ref ; ptr1 != null ; ptr1 = ptr1 . next ) { ptr2 = ptr1 . next ; while ( ptr2 != null ) { if ( ptr1 . data == ptr2 . data ) { var next = ptr2 . next ; head_ref = deleteNode ( head_ref , ptr2 ) ; ptr2 = next ; } else ptr2 = ptr2 . next ; } } return head_ref ; } function push ( head_ref , new_data ) { var new_node = new 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 ; return head_ref ; } function printList ( head ) { if ( head == null ) document . write ( " " ) ; while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = null ; head = push ( head , 12 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; document . write ( " " ) ; printList ( head ) ; head = removeDuplicates ( head ) ; document . write ( " " + " " ) ; printList ( head ) ;
Remove duplicates from an unsorted doubly linked list |  ; a node of the doubly linked list ; 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 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 ; function to remove duplicates from an unsorted doubly linked list ; if doubly linked list is empty ; unordered_set ' us ' implemented as hash table ; traverse up to the end of the list ; if current data is seen before ; store pointer to the node next to ' current ' node ; delete the node pointed to by ' current ' ; update ' current ' ; insert the current data in ' us ' ; move to the next node ; Function to insert a node at the beginning of the Doubly Linked List ; allocate node ; put in the data ; since we are adding at the beginning , prev is always null ; link the old list off the new node ; change prev of head node to new node ; move the head to povar to the new node ; Function to prvar nodes in a given doubly linked list ; if list is empty ; Driver Code ; Create the doubly linked list : 8 < -> 4 < -> 4 < -> 6 < -> 4 < -> 8 < -> 4 < -> 10 < -> 12 < -> 12 ; remove duplicate nodes
/ *javascript mplementation to remove duplicates from an unsorted doubly linked list class Node { constructor ( val ) { this . data = val ; this . prev = null ; this . next = null ; } } function deleteNode ( head_ref , del ) { if ( head_ref == null del == null ) return null ; 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 ; return head_ref ; } function removeDuplicates ( head_ref ) { if ( ( head_ref ) == null ) return null ; var us = new Set ( ) ; var current = head_ref , next ; while ( current != null ) { if ( us . has ( current . data ) ) { next = current . next ; head_ref = deleteNode ( head_ref , current ) ; current = next ; } else { us . add ( current . data ) ; current = current . next ; } } return head_ref ; } function push ( head_ref , new_data ) { var new_node = new 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 ; return head_ref ; } function printList ( head ) { if ( head == null ) document . write ( " " ) ; while ( head != null ) { document . write ( head . data + " " ) ; head = head . next ; } } var head = null ; head = push ( head , 12 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; document . write ( " " ) ; printList ( head ) ; head = removeDuplicates ( head ) ; document . write ( " " + " " ) ; printList ( head ) ;
Sorted insert in a doubly linked list with head and tail pointers | A linked list node ; Function to insetail new node ; If first node to be insetailed in doubly linked list ; If node to be insetailed has value less than first node ; If node to be insetailed has value more than last node ; Find the node before which we need to insert p . ; Insert new node before temp ; Function to prvar nodes in from left to right ; Driver code
class Node { constructor ( ) { this . info = 0 ; this . prev = null ; this . next = null ; } } var head , tail ; function nodeInsetail ( key ) { p = new Node ( ) ; p . info = key ; p . next = null ; if ( head == null ) { head = p ; tail = p ; head . prev = null ; return ; } if ( p . info < head . info ) { p . prev = null ; head . prev = p ; p . next = head ; head = p ; return ; } if ( p . info > tail . info ) { p . prev = tail ; tail . next = p ; tail = p ; return ; } temp = head . next ; while ( temp . info < p . info ) temp = temp . next ; ( temp . prev ) . next = p ; p . prev = temp . prev ; temp . prev = p ; p . next = temp ; } function printList ( temp ) { while ( temp != null ) { document . write ( temp . info + " " ) ; temp = temp . next ; } } head = tail = null ; nodeInsetail ( 30 ) ; nodeInsetail ( 50 ) ; nodeInsetail ( 90 ) ; nodeInsetail ( 10 ) ; nodeInsetail ( 40 ) ; nodeInsetail ( 110 ) ; nodeInsetail ( 60 ) ; nodeInsetail ( 95 ) ; nodeInsetail ( 23 ) ; document . write ( " " ) ; printList ( head ) ;
Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; If list is not empty Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last
function insertEnd ( value ) { if ( start == null ) { var new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } var last = ( start ) . prev ; var new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; }
Practice questions for Linked List and Recursion |
class Node { constructor ( item ) { this . data = item ; this . next = null ; } }
Find k | A BST node ; Recursive function to insert an key into BST ; Function to find k 'th largest element in BST  Here count denotes the number  of nodes processed so far ; base case ; search in left subtree ; if k 'th smallest is found in left subtree, return it ; if current element is k 'th smallest, return it ; else search in right subtree ; Function to find k 'th largest element in BST ; maintain an index to count number of nodes processed so far ; Driver code
class Node { constructor ( x ) { this . data = x ; this . left = null ; this . right = null ; } } let count = 0 ; function insert ( root , x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) root . left = insert ( root . left , x ) ; else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } function kthSmallest ( root , k ) { if ( root == null ) return null ; let left = kthSmallest ( root . left , k ) ; if ( left != null ) return left ; count ++ ; if ( count == k ) return root ; return kthSmallest ( root . right , k ) ; } function printKthSmallest ( root , k ) { count = 0 ; let res = kthSmallest ( root , k ) ; if ( res == null ) document . write ( " " + " " ) ; else document . write ( " " + " " + res . data ) ; } let root = null ; let key = [ 20 , 8 , 22 , 4 , 12 , 10 , 14 ] ; for ( let i = 0 ; i < key . length ; i ++ ) { root = insert ( root , key [ i ] ) ; } let k = 3 ; printKthSmallest ( root , k ) ;
K 'th smallest element in BST using O(1) Extra Space | A BST node ; A function to find ; Count to iterate over elements till we get the kth smallest number ; store the Kth smallest ; to store the current node ; Like Morris traversal if current does not have left child rather than printing as we did in inorder , we will just increment the count as the number will be in an increasing order ; if count is equal to K then we found the kth smallest , so store it in ksmall ; go to current 's right child ; we create links to Inorder Successor and count using these links ; building links ; link made to Inorder Successor ; While breaking the links in so made temporary threaded tree we will check for the K smallest condition ; Revert the changes made in if part ( break link from the Inorder Successor ) ; If count is equal to K then we found the kth smallest and so store it in ksmall ; return the found value ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80
class Node { constructor ( ) { this . key = 0 ; this . left = null ; this . right = null ; } } function KSmallestUsingMorris ( root , k ) { var count = 0 ; var ksmall = Number . MIN_VALUE ; var curr = root ; while ( curr != null ) { if ( curr . left == null ) { count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } else { var pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } } } return ksmall ; } function newNode ( item ) { var temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; return temp ; } function insert ( node , key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } var root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; for ( k = 1 ; k <= 7 ; k ++ ) document . write ( KSmallestUsingMorris ( root , k ) + " " ) ;
Check if an array represents Inorder of Binary Search tree or not | Function that returns true if array is Inorder traversal of any Binary Search Tree or not . ; Array has one or no element ; Unsorted pair found ; No unsorted pair found ; Drivers code
function isInorder ( arr , n ) { if ( n == 0 n == 1 ) { return true ; } for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] > arr [ i ] ) { return false ; } } return true ; } var arr = [ 19 , 23 , 25 , 30 , 45 ] ; var n = arr . length ; if ( isInorder ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Construct Tree from given Inorder and Preorder traversals | Javascript program to construct a tree using inorder and preorder traversal ; Function to build tree using given traversal ; Function to print tree in Inorder ; first recur on left child ; then print the data of node ; now recur on right child ; driver program to test above functions
class TreeNode { constructor ( x ) { this . val = x ; this . left = null ; this . right = null ; } } let set = new Set ( ) ; let stack = [ ] ; function buildTree ( preorder , inorder ) { let root = null ; for ( let pre = 0 , In = 0 ; pre < preorder . length ; ) { let node = null ; do { node = new TreeNode ( preorder [ pre ] ) ; if ( root == null ) { root = node ; } if ( stack . length != 0 ) { if ( set . has ( stack [ stack . length - 1 ] ) ) { set . delete ( stack [ stack . length - 1 ] ) ; stack . pop ( ) . right = node ; } else { stack [ stack . length - 1 ] . left = node ; } } stack . push ( node ) ; } while ( preorder [ pre ++ ] != inorder [ In ] && pre < preorder . length ) ; node = null ; while ( stack . length != 0 && In < inorder . length && stack [ stack . length - 1 ] . val == inorder [ In ] ) { node = stack . pop ( ) ; In ++ ; } if ( node != null ) { set . add ( node ) ; stack . push ( node ) ; } } return root ; } function printInorder ( node ) { if ( node == null ) return ; printInorder ( node . left ) ; document . write ( node . val + " " ) ; printInorder ( node . right ) ; } let In = [ 9 , 8 , 4 , 2 , 10 , 5 , 10 , 1 , 6 , 3 , 13 , 12 , 7 ] ; let pre = [ 1 , 2 , 4 , 8 , 9 , 5 , 10 , 10 , 3 , 6 , 7 , 12 , 13 ] ; let len = In . length ; let root = buildTree ( pre , In ) ; printInorder ( root ) ;
Check if two BSTs contain same set of elements | BST Node ; Utility function to create new Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two vectors and store inorder traversals of both BSTs in them . ; Return true if both vectors are identical ; First BST ; Second BST ; check if two BSTs have same set of elements
class Node { constructor ( ) { this . data = 0 ; this . prev = null ; this . next = null ; } } function newNode ( val ) { var temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } function storeInorder ( root , v ) { if ( root == null ) return ; storeInorder ( root . left , v ) ; v . push ( root . data ) ; storeInorder ( root . right , v ) ; } function checkBSTs ( root1 , root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; var v1 = [ ] ; var v2 = [ ] ; storeInorder ( root1 , v1 ) ; storeInorder ( root2 , v2 ) ; return ( v1 == v2 ) ; } var root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; var root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Shortest distance between two nodes in BST | JavaScript program to find distance between two nodes in BST ; Standard BST insert function ; This function returns distance of x from root . This function assumes that x exists in BST and BST is not NULL . ; Returns minimum distance beween a and b . This function assumes that a and b exist in BST . ; Both keys lie in left ; Both keys lie in right same path ; Lie in opposite directions ( Root is LCA of two nodes ) ; This function make sure that a is smaller than b before making a call to findDistWrapper ( ) ; Driver code
class Node { constructor ( ) { this . key = 0 ; this . left = null ; this . right = null ; } } function newNode ( key ) { var ptr = new Node ( ) ; ptr . key = key ; ptr . left = null ; ptr . right = null ; return ptr ; } function insert ( root , key ) { if ( root == null ) root = newNode ( key ) ; else if ( root . key > key ) root . left = insert ( root . left , key ) ; else if ( root . key < key ) root . right = insert ( root . right , key ) ; return root ; } function distanceFromRoot ( root , x ) { if ( root . key == x ) return 0 ; else if ( root . key > x ) return 1 + distanceFromRoot ( root . left , x ) ; return 1 + distanceFromRoot ( root . right , x ) ; } function distanceBetween2 ( root , a , b ) { if ( root == null ) return 0 ; if ( root . key > a && root . key > b ) return distanceBetween2 ( root . left , a , b ) ; if ( root . key < a && root . key < b ) return distanceBetween2 ( root . right , a , b ) ; if ( root . key >= a && root . key <= b ) return distanceFromRoot ( root , a ) + distanceFromRoot ( root , b ) ; return 0 ; } function findDistWrapper ( root , a , b ) { var temp = 0 ; if ( a > b ) { temp = a ; a = b ; b = temp ; } return distanceBetween2 ( root , a , b ) ; } var root = null ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; document . write ( findDistWrapper ( root , 5 , 35 ) ) ;