codeparrot/xlcost-text-to-code huggingface dataset:
Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | C # program to illustrate inserting a Node in a Cicular Doubly Linked list in begging , end and middle ; Structure of a Node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; 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 to insert Node at the beginning of the List , ; Pointer points to last Node ; Inserting the data ; setting up previous and next of new node ; Update next and previous pointers of start and last . ; Update start pointer ; Function to insert node with value as value1 . The new node is inserted after the node with with value2 ; Inserting the data ; Find node having value2 and next node of it ; insert new_node between temp and next . ; Driver code ; Start with the empty list ; Insert 5. So linked list becomes 5. null ; Insert 4 at the beginning . So linked list becomes 4.5 ; Insert 7 at the end . So linked list becomes 4.5 . 7 ; Insert 8 at the end . So linked list becomes 4.5 . 7.8 ; Insert 6 , after 5. So linked list becomes 4.5 . 6.7 . 8
using System ; using System . Collections . Generic ; class GFG { static Node start ; public class Node { public int data ; public Node next ; public Node prev ; } ; static void insertEnd ( int value ) { Node new_node ; if ( start == null ) { new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; } static void insertBegin ( int value ) { Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; new_node . prev = last ; last . next = ( start ) . prev = new_node ; start = new_node ; } static void insertAfter ( int value1 , int value2 ) { Node new_node = new Node ( ) ; new_node . data = value1 ; Node temp = start ; while ( temp . data != value2 ) temp = temp . next ; Node next = temp . next ; temp . next = new_node ; new_node . prev = temp ; new_node . next = next ; next . prev = new_node ; } static void display ( ) { Node temp = start ; Console . Write ( " in forward direction " while ( temp . next != start ) { Console . Write ( " { 0 } ▁ " , temp . data ) ; = temp . next ; } Console . Write ( " { 0 } ▁ " , temp . data ) ; Console . Write ( " in reverse direction " Node last = start . prev ; temp = last ; while ( temp . prev != last ) { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . prev ; } Console . Write ( " { 0 } ▁ " , temp . data ) ; } public static void Main ( String [ ] args ) { Node start = null ; insertEnd ( 5 ) ; insertBegin ( 4 ) ; insertEnd ( 7 ) ; insertEnd ( 8 ) ; insertAfter ( 6 , 5 ) ; Console . Write ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) ; display ( ) ; } }
Boundary Traversal of binary tree | C # program to print boundary traversal of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A simple function to print leaf nodes of a binary tree ; Print it if it is a leaf node ; A function to print all left boundary nodes , except a leaf node . Print the nodes in TOP DOWN manner ; to ensure top down order , print the node before calling itself for left subtree ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to print all right boundary nodes , except a leaf node . Print the nodes in BOTTOM UP manner ; to ensure bottom up order , first call for right subtree , then print this node ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to do boundary traversal of a given binary tree ; Print the left boundary in top - down manner . ; Print all leaf nodes ; Print the right boundary in bottom - up manner ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void printLeaves ( Node node ) { if ( node == null ) return ; printLeaves ( node . left ) ; if ( node . left == null && node . right == null ) { Console . Write ( node . data + " ▁ " ) ; } printLeaves ( node . right ) ; } public virtual void printBoundaryLeft ( Node node ) { if ( node == null ) return ; if ( node . left != null ) { Console . Write ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; } else if ( node . right != null ) { Console . Write ( node . data + " ▁ " ) ; printBoundaryLeft ( node . right ) ; } } public virtual void printBoundaryRight ( Node node ) { if ( node == null ) return ; if ( node . right != null ) { printBoundaryRight ( node . right ) ; Console . Write ( node . data + " ▁ " ) ; } else if ( node . left != null ) { printBoundaryRight ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; } } public virtual void printBoundary ( Node node ) { if ( node == null ) return ; Console . Write ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; printLeaves ( node . left ) ; printLeaves ( node . right ) ; printBoundaryRight ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; tree . root . right = new Node ( 22 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printBoundary ( tree . root ) ; } }
An interesting method to print reverse of a linked list | C # program to print reverse of list ; Represents node of a linkedlist ; Function to reverse the linked list ; For each node , print proper number of spaces before printing it ; use of carriage return to move back and print . ; Function to push a node ; Function to print linked list and find its length ; i for finding length of list ; Driver code ; Start with the empty list ; list nodes are as 6 5 4 3 2 1 ; printlist print the list and return the size of list ; print reverse list with help of carriage return function
using System ; public class Node { public int data ; public Node next ; public Node ( int val ) { data = val ; next = null ; } } public class GFG { static void printReverse ( Node head , int n ) { int j = 0 ; Node current = head ; while ( current != null ) { for ( int i = 0 ; i < 2 * ( n - j ) ; i ++ ) Console . Write ( " ▁ " ) ; Console . Write ( " \r " + current . data ) ; current = current . next ; j ++ ; } } static Node push ( Node head , int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; return head ; } static int printList ( Node head ) { int i = 0 ; Node temp = head ; while ( temp != null ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; i ++ ; } return i ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 3 ) ; head = push ( head , 4 ) ; head = push ( head , 5 ) ; head = push ( head , 6 ) ; Console . WriteLine ( " Given ▁ linked ▁ list : ▁ " ) ; int n = printList ( head ) ; Console . WriteLine ( " Reversed ▁ Linked ▁ list : ▁ " ) ; printReverse ( head , n ) ; Console . WriteLine ( ) ; } }
Practice questions for Linked List and Recursion |
public class Node { public int data ; public Node next ; } ;
Practice questions for Linked List and Recursion |
static void fun1 ( Node head ) { if ( head == null ) { return ; } fun1 ( head . next ) ; Console . Write ( head . data + " ▁ " ) ; }
Practice questions for Linked List and Recursion |
static void fun2 ( Node head ) { if ( head == null ) { return ; } Console . Write ( head . data + " ▁ " ) ; if ( head . next != null ) { fun2 ( head . next . next ) ; } Console . Write ( head . data + " ▁ " ) ; }
Practice questions for Linked List and Recursion | C # code implementation for above approach ; A linked list node ; Prints a linked list in reverse manner ; prints alternate nodes of a Linked List , first from head to end , and then from end to head . ; Given a reference ( pointer to pointer ) to the head of a list and an int , . Push a new node on the front of the list . ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver code ; Start with the empty list ; Using . Push ( ) to conbelow list 1 -> 2 -> 3 -> 4 -> 5
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static void fun1 ( Node head ) { if ( head == null ) { return ; } fun1 ( head . next ) ; Console . Write ( head . data + " ▁ " ) ; } static void fun2 ( Node start ) { if ( start == null ) { return ; } Console . Write ( start . data + " ▁ " ) ; if ( start . next != null ) { fun2 ( start . next . next ) ; } Console . Write ( start . data + " ▁ " ) ; } static Node 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 ; return head_ref ; } public static void Main ( String [ ] args ) { Node head = null ; head = Push ( head , 5 ) ; head = Push ( head , 4 ) ; head = Push ( head , 3 ) ; head = Push ( head , 2 ) ; head = Push ( head , 1 ) ; Console . Write ( " Output ▁ of ▁ fun1 ( ) ▁ for ▁ " + " list ▁ 1 - > 2 - > 3 - > 4 - > 5 ▁ STRNEWLINE " ) ; fun1 ( head ) ; Console . Write ( " fun2 ( ) for " ▁ + ▁ " 1 -> 2 -> 3 -> 4 -> 5 " ) ; fun2 ( head ) ; } }
Density of Binary Tree in One Traversal | C # program to find density of Binary Tree ; A binary tree node ; Class to implement pass by reference of size ; Function to compute height and size of a binary tree ; compute height of each subtree ; increase size by 1 ; return larger of the two ; function to calculate density of a binary tree ; Finds height and size ; Driver code
using System ; class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class Size { public int size = 0 ; } class BinaryTree { Node root ; int heighAndSize ( Node node , Size size ) { if ( node == null ) return 0 ; int l = heighAndSize ( node . left , size ) ; int r = heighAndSize ( node . right , size ) ; size . size ++ ; return ( l > r ) ? l + 1 : r + 1 ; } float density ( Node root ) { Size size = new Size ( ) ; if ( root == null ) return 0 ; int _height = heighAndSize ( root , size ) ; return ( float ) size . size / _height ; } static public void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; Console . WriteLine ( " Density ▁ of ▁ given ▁ " + " Binary ▁ Tree ▁ is ▁ : ▁ " + tree . density ( tree . root ) ) ; } }
Squareroot ( n ) | C # program to find sqrt ( n ) 'th node  of a linked list  ; Linked list node ; Function to get the sqrt ( n ) th node of a linked list ; Traverse the list ; check if j = sqrt ( i ) ; for first node ; increment j if j = sqrt ( i ) ; return node 's data  ; function to add a new node at the beginning of the list ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver code ; Start with the empty list
using System ; public class GfG { class Node { public int data ; public Node next ; } static Node head = null ; static int printsqrtn ( Node head ) { Node sqrtn = null ; int i = 1 , j = 1 ; while ( head != null ) { if ( i == j * j ) { if ( sqrtn == null ) sqrtn = head ; else sqrtn = sqrtn . next ; j ++ ; } i ++ ; head = head . next ; } return sqrtn . data ; } static void print ( Node head ) { while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; head = head . next ; } Console . WriteLine ( ) ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } public static void Main ( String [ ] args ) { push ( 40 ) ; push ( 30 ) ; push ( 20 ) ; push ( 10 ) ; Console . Write ( " Given ▁ linked ▁ list ▁ is : " ) ; print ( head ) ; Console . Write ( " sqrt ( n ) th ▁ node ▁ is ▁ " + printsqrtn ( head ) ) ; } }
Find the fractional ( or n / k | C # program to find fractional node in a linked list ; Linked list node ; Constructor ; Function to find fractional node in the linked list ; Corner cases ; Traverse the given list ; For every k nodes , we move fractionalNode one step ahead . ; First time we see a multiple of k ; A utility function to print a linked list ; Driver code
using System ; public class FractionalNodell { public class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; } } static Node fractionalNodes ( Node head , int k ) { if ( k <= 0 head == null ) return null ; Node fractionalNode = null ; int i = 0 ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) { if ( fractionalNode == null ) fractionalNode = head ; else fractionalNode = fractionalNode . next ; } i ++ ; } return fractionalNode ; } static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . next ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Console . Write ( " List ▁ is ▁ " ) ; printList ( head ) ; Node answer = fractionalNodes ( head , k ) ; Console . WriteLine ( " Fractional ▁ node ▁ is ▁ " + answer . data ) ; } }
Find modular node in a linked list | C # program to find modular node in a linked list ; A Linkedlist node ; Function to find modular node in the linked list ; Corner cases ; Traverse the given list ; Driver code
using System ; class GFG { public class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void Main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; Console . Write ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) Console . WriteLine ( answer . data ) ; else Console . WriteLine ( " null " ) ; } }
Modify contents of Linked List | C # implementation to modify the contents of the linked list ; Linked list node ; Function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list at the end of the new node ; move the head to point to the new node ; function to print the linked list ; Function to middle node of list . ; Advance ' fast ' two nodes , and advance ' slow ' one node ; If number of nodes are odd then update slow by slow . next ; ; function to modify the contents of the linked list . ; Create Stack . ; Traverse the list by using temp until stack is empty . ; Driver code ; creating the linked list ; Call Function to Find the starting point of second half of list . ; Call function to modify the contents of the linked list . ; print the modified linked list
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node 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 ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) { return ; } while ( head . next != null ) { Console . Write ( head . data + " - > " ) ; head = head . next ; } Console . Write ( head . data + " STRNEWLINE " ) ; } static Node find_mid ( Node head ) { Node temp = head , slow = head , fast = head ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } if ( fast != null ) { slow = slow . next ; } return slow ; } static void modifyTheList ( Node head , Node slow ) { Stack < int > s = new Stack < int > ( ) ; Node temp = head ; while ( slow != null ) { s . Push ( slow . data ) ; slow = slow . next ; } while ( s . Count != 0 ) { temp . data = temp . data - s . Peek ( ) ; temp = temp . next ; s . Pop ( ) ; } } public static void Main ( String [ ] args ) { Node head = null , mid ; head = push ( head , 10 ) ; head = push ( head , 7 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 2 ) ; mid = find_mid ( head ) ; modifyTheList ( head , mid ) ; Console . Write ( " Modified ▁ List : " + " STRNEWLINE " ) ; printList ( head ) ; } }
Binary Search Tree | Set 1 ( Search and Insertion ) | A utility function to search a given key in BST ; Base Cases : root is null or key is present at root ; Key is greater than root 's key ; Key is smaller than root 's key
public Node search ( Node root , int key ) { if ( root == null root . key == key ) return root ; if ( root . key < key ) return search ( root . right , key ) ; return search ( root . left , key ) ; }
Construct BST from given preorder traversal | Set 1 | Construct a BST from given pre - order traversal for example if the given traversal is { 10 , 5 , 1 , 7 , 40 , 50 } , then the output should be the root of the following tree . 10 / \ 5 40 / \ \ 1 7 50 ; This will create the BST ; A wrapper function of createNode ; A function to print BST in inorder ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; this . left = this . right = null ; } } public class CreateBSTFromPreorder { private static Node node ; public static Node createNode ( Node node , int data ) { if ( node == null ) node = new Node ( data ) ; if ( node . data > data ) node . left = createNode ( node . left , data ) ; if ( node . data < data ) node . right = createNode ( node . right , data ) ; return node ; } public static void create ( int data ) { node = createNode ( node , data ) ; } public static void inorderRec ( Node root ) { if ( root != null ) { inorderRec ( root . left ) ; Console . WriteLine ( root . data ) ; inorderRec ( root . right ) ; } } public static void Main ( String [ ] args ) { int [ ] nodeData = { 10 , 5 , 1 , 7 , 40 , 50 } ; for ( int i = 0 ; i < nodeData . Length ; i ++ ) { create ( nodeData [ i ] ) ; } inorderRec ( node ) ; } }
Modify a binary tree to get preorder traversal using right pointers only | C # code to modify binary tree for traversal using only right pointer ; A binary tree node has data , left child and right child ; function that allocates a new node with the given data and null left and right pointers . ; Function to modify tree ; if the left tree exists ; get the right - most of the original left subtree ; set root right to left subtree ; if the right subtree does not exists we are done ! ; set right pointer of right - most of the original left subtree ; modify the rightsubtree ; printing using right pointer only ; Driver cde ; Constructed binary tree is 10 / \ 8 2 / \ 3 5
using System ; class GFG { public class Node { public int data ; public Node left ; public Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static Node modifytree ( Node root ) { Node right = root . right ; Node rightMost = root ; if ( root . left != null ) { rightMost = modifytree ( root . left ) ; root . right = root . left ; root . left = null ; } if ( right == null ) return rightMost ; rightMost . right = right ; rightMost = modifytree ( right ) ; return rightMost ; } static void printpre ( Node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . right ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }
Construct BST from its given level order traversal | C # implementation to construct a BST from its level order traversal ; node of a BST ; function to get a new node ; Allocate memory ; put in the data ; function to construct a BST from its level order traversal ; function to print the inorder traversal ; Driver code
using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static Node LevelOrder ( Node root , int data ) { if ( root == null ) { root = getNode ( data ) ; return root ; } if ( data <= root . data ) root . left = LevelOrder ( root . left , data ) ; else root . right = LevelOrder ( root . right , data ) ; return root ; } static Node constructBst ( int [ ] arr , int n ) { if ( n == 0 ) return null ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) root = LevelOrder ( root , arr [ i ] ) ; return root ; } static void inorderTraversal ( Node root ) { if ( root == null ) return ; inorderTraversal ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorderTraversal ( root . right ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . Length ; Node root = constructBst ( arr , n ) ; Console . Write ( " Inorder ▁ Traversal : ▁ " ) ; inorderTraversal ( root ) ; } }
Modify a binary tree to get preorder traversal using right pointers only | C # code to modify binary tree for traversal using only right pointer ; A binary tree node has data , left child and right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; An iterative process to set the right pointer of Binary tree ; Base Case ; Create an empty stack and Push root to it ; Pop all items one by one . Do following for every Popped item a ) print it b ) Push its right child c ) Push its left child Note that right child is Pushed first so that left is processed first ; Pop the top item from stack ; Push right and left children of the Popped node to stack ; check if some previous node exists ; set the right pointer of previous node to currrent ; set previous node as current node ; printing using right pointer only ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ 3 5
using System ; using System . Collections ; class GfG { public class Node { public int data ; public Node left ; public Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void modifytree ( Node root ) { if ( root == null ) return ; Stack nodeStack = new Stack ( ) ; nodeStack . Push ( root ) ; Node pre = null ; while ( nodeStack . Count != 0 ) { Node node = ( Node ) nodeStack . Peek ( ) ; nodeStack . Pop ( ) ; if ( node . right != null ) nodeStack . Push ( node . right ) ; if ( node . left != null ) nodeStack . Push ( node . left ) ; if ( pre != null ) { pre . right = node ; } pre = node ; } } static void printpre ( Node root ) { while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; root = root . right ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }