codeparrot/xlcost-text-to-code huggingface dataset:
Two Clique Problem ( Check if Graph can be divided in two Cliques ) | C ++ program to find out whether a given graph can be converted to two Cliques or not . ; This function returns true if subgraph reachable from src is Bipartite or not . ; Create a queue ( FIFO ) of vertex numbers and enqueue source vertex for BFS traversal ; Run while there are vertices in queue ( Similar to BFS ) ; Dequeue a vertex from queue ; Find all non - colored adjacent vertices ; An edge from u to v exists and destination v is not colored ; Assign alternate color to this adjacent v of u ; An edge from u to v exists and destination v is colored with same color as u ; If we reach here , then all adjacent vertices can be colored with alternate color ; Returns true if a Graph G [ ] [ ] is Bipartite or not . Note that G may not be connected . ; Create a color array to store colors assigned to all veritces . Vertex number is used as index in this array . The value ' - 1' of colorArr [ i ] is used to indicate that no color is assigned to vertex ' i ' . The value 1 is used to indicate first color is assigned and value 0 indicates second color is assigned . ; One by one check all not yet colored vertices . ; Returns true if G can be divided into two Cliques , else false . ; Find complement of G [ ] [ ] All values are complemented except diagonal ones ; Return true if complement is Bipartite else false . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int V = 5 ; bool isBipartiteUtil ( int G [ ] [ V ] , int src , int colorArr [ ] ) { colorArr [ src ] = 1 ; queue < int > q ; q . push ( src ) ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; for ( int v = 0 ; v < V ; ++ v ) { if ( G [ u ] [ v ] && colorArr [ v ] == -1 ) { colorArr [ v ] = 1 - colorArr [ u ] ; q . push ( v ) ; } else if ( G [ u ] [ v ] && colorArr [ v ] == colorArr [ u ] ) return false ; } } return true ; } bool isBipartite ( int G [ ] [ V ] ) { int colorArr [ V ] ; for ( int i = 0 ; i < V ; ++ i ) colorArr [ i ] = -1 ; for ( int i = 0 ; i < V ; i ++ ) if ( colorArr [ i ] == -1 ) if ( isBipartiteUtil ( G , i , colorArr ) == false ) return false ; return true ; } bool canBeDividedinTwoCliques ( int G [ ] [ V ] ) { int GC [ V ] [ V ] ; for ( int i = 0 ; i < V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) GC [ i ] [ j ] = ( i != j ) ? ! G [ i ] [ j ] : 0 ; return isBipartite ( GC ) ; } int main ( ) { int G [ ] [ V ] = { { 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 0 , 0 } , { 1 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 , 0 } } ; canBeDividedinTwoCliques ( G ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Check whether given degrees of vertices represent a Graph or Tree | C ++ program to check whether input degree sequence is graph or tree ; Function returns true for tree false for graph ; Find sum of all degrees ; Graph is tree if sum is equal to 2 ( n - 1 ) ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) deg_sum += degree [ i ] ; return ( 2 * ( n - 1 ) == deg_sum ) ; } int main ( ) { int n = 5 ; int degree [ ] = { 2 , 3 , 1 , 1 , 1 } ; if ( check ( degree , n ) ) cout << " Tree " ; else cout << " Graph " ; return 0 ; }
Finding minimum vertex cover size of a graph using binary search | A C ++ program to find size of minimum vertex cover using Binary Search ; Global array to store the graph Note : since the array is global , all the elements are 0 initially ; Returns true if there is a possible subset of size ' k ' that can be a vertex cover ; Set has first ' k ' bits high initially ; to mark the edges covered in each subset of size ' k ' ; Reset visited array for every subset of vertices ; set counter for number of edges covered from this subset of vertices to zero ; selected vertex cover is the indices where ' set ' has its bit high ; Mark all edges emerging out of this vertex visited ; If the current subset covers all the edges ; Generate previous combination with k bits high set & - set = ( 1 << last bit high in set ) ; Returns answer to graph stored in gr [ ] [ ] ; Binary search the answer ; at the end of while loop both left and right will be equal , / as when they are not , the while loop won 't exit the minimum  size vertex cover = left = right ; Inserts an edge in the graph ; Undirected graph ; Driver code ; 6 / 1 -- -- - 5 vertex cover = { 1 , 2 } / | \ 3 | \  \ | \ 2 4 ; Let us create another graph ; 2 -- -- 4 -- -- 6 / | | 1 | | vertex cover = { 2 , 3 , 4 } \ | | 3 -- -- 5
#include <bits/stdc++.h> NEW_LINE #define maxn  25 NEW_LINE using namespace std ; bool gr [ maxn ] [ maxn ] ; bool isCover ( int V , int k , int E ) { int set = ( 1 << k ) - 1 ; int limit = ( 1 << V ) ; bool vis [ maxn ] [ maxn ] ; while ( set < limit ) { memset ( vis , 0 , sizeof vis ) ; int cnt = 0 ; for ( int j = 1 , v = 1 ; j < limit ; j = j << 1 , v ++ ) { if ( set & j ) { for ( int k = 1 ; k <= V ; k ++ ) { if ( gr [ v ] [ k ] && ! vis [ v ] [ k ] ) { vis [ v ] [ k ] = 1 ; vis [ k ] [ v ] = 1 ; cnt ++ ; } } } } if ( cnt == E ) return true ; int c = set & - set ; int r = set + c ; set = ( ( ( r ^ set ) >> 2 ) / c ) | r ; } return false ; } int findMinCover ( int n , int m ) { int left = 1 , right = n ; while ( right > left ) { int mid = ( left + right ) >> 1 ; if ( isCover ( n , mid , m ) == false ) left = mid + 1 ; else right = mid ; } return left ; } void insertEdge ( int u , int v ) { gr [ u ] [ v ] = 1 ; gr [ v ] [ u ] = 1 ; } int main ( ) { int V = 6 , E = 6 ; insertEdge ( 1 , 2 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 1 , 3 ) ; insertEdge ( 1 , 4 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; cout << " Minimum ▁ size ▁ of ▁ a ▁ vertex ▁ cover ▁ = ▁ " << findMinCover ( V , E ) << endl ; memset ( gr , 0 , sizeof gr ) ; V = 6 , E = 7 ; insertEdge ( 1 , 2 ) ; insertEdge ( 1 , 3 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 3 , 5 ) ; insertEdge ( 4 , 5 ) ; insertEdge ( 4 , 6 ) ; cout << " Minimum ▁ size ▁ of ▁ a ▁ vertex ▁ cover ▁ = ▁ " << findMinCover ( V , E ) << endl ; return 0 ; }
Morris traversal for Preorder | C ++ program for Morris Preorder traversal ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Preorder traversal without recursion and without stack ; If left child is null , print the current node data . Move to right child . ; Find inorder predecessor ; If the right child of inorder predecessor already points to this node ; If right child doesn 't point to this node, then print this   node and make right child point to this node  ; Function for sStandard preorder traversal ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left , * right ; } ; node * newNode ( int data ) { node * temp = new node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void morrisTraversalPreorder ( node * root ) { while ( root ) { if ( root -> left == NULL ) { cout << root -> data << " ▁ " ; root = root -> right ; } else { node * current = root -> left ; while ( current -> right && current -> right != root ) current = current -> right ; if ( current -> right == root ) { current -> right = NULL ; root = root -> right ; } else { cout << root -> data << " ▁ " ; current -> right = root ; root = root -> left ; } } } } void preorder ( node * root ) { if ( root ) { cout << root -> data << " ▁ " ; preorder ( root -> left ) ; preorder ( root -> right ) ; } } int main ( ) { node * root = NULL ; root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> left -> left -> left = newNode ( 8 ) ; root -> left -> left -> right = newNode ( 9 ) ; root -> left -> right -> left = newNode ( 10 ) ; root -> left -> right -> right = newNode ( 11 ) ; morrisTraversalPreorder ( root ) ; cout << endl ; preorder ( root ) ; return 0 ; }
Sum of dependencies in a graph | C ++ program to find the sum of dependencies ; To add an edge ; find the sum of all dependencies ; just find the size at each vector 's index ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addEdge ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; } int findSum ( vector < int > adj [ ] , int V ) { int sum = 0 ; for ( int u = 0 ; u < V ; u ++ ) sum += adj [ u ] . size ( ) ; return sum ; } int main ( ) { int V = 4 ; vector < int > adj [ V ] ; addEdge ( adj , 0 , 2 ) ; addEdge ( adj , 0 , 3 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 2 , 3 ) ; cout << " Sum ▁ of ▁ dependencies ▁ is ▁ " << findSum ( adj , V ) ; return 0 ; }
Linked List | Set 2 ( Inserting a node ) | Given a reference ( pointer to pointer ) to the head of a list and an int , inserts a new node on the front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new node as head ; 4. move the head to point to the new node
void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; }
Linked List | Set 2 ( Inserting a node ) | Given a node prev_node , insert a new node after the given prev_node ; 1. Check if the given prev_node is NULL ; 2. Allocate the Node & 3. Put in the data ; 4. Make next of new node as next of prev_node ; 5. move the next of prev_node as new_node
void insertAfter ( Node * prev_node , int new_data ) { if ( prev_node == NULL ) { cout << " the ▁ given ▁ previous ▁ node ▁ cannot ▁ be ▁ NULL " ; return ; } Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = prev_node -> next ; prev_node -> next = new_node ; }
Iterative Preorder Traversal | C ++ program to implement iterative preorder traversal ; 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 print preorder traversal 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 and print it ; Push right and left children of the popped node to stack ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; struct node * newNode ( int data ) { struct node * node = new struct node ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } void iterativePreorder ( node * root ) { if ( root == NULL ) return ; stack < node * > nodeStack ; nodeStack . push ( root ) ; while ( nodeStack . empty ( ) == false ) { struct node * node = nodeStack . top ( ) ; printf ( " % d ▁ " , node -> data ) ; nodeStack . pop ( ) ; if ( node -> right ) nodeStack . push ( node -> right ) ; if ( node -> left ) nodeStack . push ( node -> left ) ; } } int main ( ) { struct node * root = newNode ( 10 ) ; root -> left = newNode ( 8 ) ; root -> right = newNode ( 2 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 2 ) ; iterativePreorder ( root ) ; return 0 ; }
Iterative Preorder Traversal |  ; Tree Node ; Iterative function to do Preorder traversal of the tree ; start from root node ( set current node to root node ) ; run till stack is not empty or current is not NULL ; Print left children while exist and keep pushing right into the stack . ; We reach when curr is NULL , so We take out a right child from stack ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; Node ( int data ) { this -> data = data ; this -> left = this -> right = NULL ; } } ; void preorderIterative ( Node * root ) { if ( root == NULL ) return ; stack < Node * > st ; Node * curr = root ; while ( ! st . empty ( ) curr != NULL ) { while ( curr != NULL ) { cout << curr -> data << " ▁ " ; if ( curr -> right ) st . push ( curr -> right ) ; curr = curr -> left ; } if ( st . empty ( ) == false ) { curr = st . top ( ) ; st . pop ( ) ; } } } int main ( ) { Node * root = new Node ( 10 ) ; root -> left = new Node ( 20 ) ; root -> right = new Node ( 30 ) ; root -> left -> left = new Node ( 40 ) ; root -> left -> left -> left = new Node ( 70 ) ; root -> left -> right = new Node ( 50 ) ; root -> right -> left = new Node ( 60 ) ; root -> left -> left -> right = new Node ( 80 ) ; preorderIterative ( root ) ; return 0 ; }
Write a function that counts the number of times a given int occurs in a Linked List | C ++ program to count occurrences in a linked list using recursion ; Link list node ; global variable for counting frequeancy of given element k ; 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 ; link the old list off the new node ; move the head to point to the new node ; Counts the no . of occurrences of a node ( search_for ) in a linked list ( head ) ; Driver program to test count function ; Start with the empty list ; Use push ( ) to construct below list 1 -> 2 -> 1 -> 3 -> 1 ; Check the count function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; int frequency = 0 ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int count ( struct Node * head , int key ) { if ( head == NULL ) return frequency ; if ( head -> data == key ) frequency ++ ; return count ( head -> next , key ) ; } int main ( ) { struct Node * head = NULL ; push ( & head , 1 ) ; push ( & head , 3 ) ; push ( & head , 1 ) ; push ( & head , 2 ) ; push ( & head , 1 ) ; cout << " count ▁ of ▁ 1 ▁ is ▁ " << count ( head , 1 ) ; return 0 ; }
Write a function that counts the number of times a given int occurs in a Linked List | Counts the no . of occurrences of a node ( search_for ) in a linked list ( head )
int count ( struct Node * head , int key ) { if ( head == NULL ) return 0 ; if ( head -> data == key ) return 1 + count ( head -> next , key ) ; return count ( head -> next , key ) ; }
Detect loop in a linked list | C ++ program to detect loop in a linked list ; Link list node ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Returns true if there is a loop in linked list else returns false . ; If this node is already traverse it means there is a cycle ( Because you we encountering the node for the second time ) . ; If we are seeing the node for the first time , mark its flag as 1 ; Driver program to test above function ; Start with the empty list ; Create a loop for testing
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; int flag ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = new Node ; new_node -> data = new_data ; new_node -> flag = 0 ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } bool detectLoop ( struct Node * h ) { while ( h != NULL ) { if ( h -> flag == 1 ) return true ; h -> flag = 1 ; h = h -> next ; } return false ; } int main ( ) { struct Node * head = NULL ; push ( & head , 20 ) ; push ( & head , 4 ) ; push ( & head , 15 ) ; push ( & head , 10 ) ; head -> next -> next -> next -> next = head ; if ( detectLoop ( head ) ) cout << " Loop ▁ found " ; else cout << " No ▁ Loop " ; return 0 ; }
Detect loop in a linked list | C ++ program to detect loop in a linked list ; Link list node ; 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 ; Create a loop for testing
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int detectLoop ( Node * list ) { Node * slow_p = list , * fast_p = list ; while ( slow_p && fast_p && fast_p -> next ) { slow_p = slow_p -> next ; fast_p = fast_p -> next -> next ; if ( slow_p == fast_p ) { return 1 ; } } return 0 ; } int main ( ) { Node * head = NULL ; push ( & head , 20 ) ; push ( & head , 4 ) ; push ( & head , 15 ) ; push ( & head , 10 ) ; head -> next -> next -> next -> next = head ; if ( detectLoop ( head ) ) cout << " Loop ▁ found " ; else cout << " No ▁ Loop " ; return 0 ; }
Detect loop in a linked list | C ++ program to return first node of loop ; A utility function to print a linked list ; Function to detect first node of loop in a linked list that may contain loop ; Create a temporary node ; This condition is for the case when there is no loop ; Check if next is already pointing to temp ; Store the pointer to the next node in order to get to it in the next step ; Make next point to temp ; Get to the next node in the list ; Driver program to test above function ; Create a loop for testing ( 5 is pointing to 3 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * next ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> next = NULL ; return temp ; } void printList ( Node * head ) { while ( head != NULL ) { cout << head -> key << " ▁ " ; head = head -> next ; } cout << endl ; } bool detectLoop ( Node * head ) { Node * temp = new Node ; while ( head != NULL ) { if ( head -> next == NULL ) { return false ; } if ( head -> next == temp ) { return true ; } Node * nex = head -> next ; head -> next = temp ; head = nex ; } return false ; } int main ( ) { Node * head = newNode ( 1 ) ; head -> next = newNode ( 2 ) ; head -> next -> next = newNode ( 3 ) ; head -> next -> next -> next = newNode ( 4 ) ; head -> next -> next -> next -> next = newNode ( 5 ) ; head -> next -> next -> next -> next -> next = head -> next -> next ; bool found = detectLoop ( head ) ; if ( found ) cout << " Loop ▁ Found " ; else cout << " No ▁ Loop " ; return 0 ; }
Detect loop in a linked list | C ++ program to return first node of loop ; A utility function to print a linked list ; returns distance between first and last node every time * last node moves forwars ; counts no of nodes between first and last ; Function to detect first node of loop in a linked list that may contain loop ; Create a temporary node ; first always points to head ; last pointer initially points to head ; current_length stores no of nodes between current * position of first and last ; current_length stores no of nodes between previous * position of first and last ; set prev_length to current length then update the current length ; distance is calculated ; last node points the next node ; Driver program to test above function ; Create a loop for testing ( 5 is pointing to 3 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * next ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> next = NULL ; return temp ; } void printList ( Node * head ) { while ( head != NULL ) { cout << head -> key << " ▁ " ; head = head -> next ; } cout << endl ; } int distance ( Node * first , Node * last ) { int counter = 0 ; Node * curr ; curr = first ; while ( curr != last ) { counter += 1 ; curr = curr -> next ; } return counter + 1 ; } bool detectLoop ( Node * head ) { Node * temp = new Node ; Node * first , * last ; first = head ; last = head ; int current_length = 0 ; int prev_length = -1 ; while ( current_length > prev_length && last != NULL ) { prev_length = current_length ; current_length = distance ( first , last ) ; last = last -> next ; } if ( last == NULL ) { return false ; } else { return true ; } } int main ( ) { Node * head = newNode ( 1 ) ; head -> next = newNode ( 2 ) ; head -> next -> next = newNode ( 3 ) ; head -> next -> next -> next = newNode ( 4 ) ; head -> next -> next -> next -> next = newNode ( 5 ) ; head -> next -> next -> next -> next -> next = head -> next -> next ; bool found = detectLoop ( head ) ; if ( found ) cout << " Loop ▁ Found " ; else cout << " No ▁ Loop ▁ Found " ; return 0 ; }
Remove duplicates from a sorted linked list | CPP program for the above approach ; Function to insert a node at the beginging of the linked * list ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Function to print nodes in a given linked list ; Function to remove duplicates ; Driver Code ; Created linked list will be 11 -> 11 -> 11 -> 13 -> 13 -> 20
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; Node ( ) { data = 0 ; next = NULL ; } } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( Node * node ) { while ( node != NULL ) { cout << node -> data << " ▁ " ; node = node -> next ; } } void removeDuplicates ( Node * head ) { unordered_map < int , bool > track ; Node * temp = head ; while ( temp ) { if ( track . find ( temp -> data ) == track . end ( ) ) { cout << temp -> data << " ▁ " ; } track [ temp -> data ] = true ; temp = temp -> next ; } } int main ( ) { Node * head = NULL ; push ( & head , 20 ) ; push ( & head , 13 ) ; push ( & head , 13 ) ; push ( & head , 11 ) ; push ( & head , 11 ) ; push ( & head , 11 ) ; cout << " Linked ▁ list ▁ before ▁ duplicate ▁ removal ▁ " ; printList ( head ) ; cout << " Linked list after duplicate removal " ; removeDuplicates ( head ) ; return 0 ; }
Swap nodes in a linked list without swapping data | This program swaps the nodes of linked list rather than swapping the field from the nodes . ; Function to swap nodes x and y in linked list by changing links ; Nothing to do if x and y are same ; Search for x ( keep track of prevX and CurrX ; Search for y ( keep track of prevY and CurrY ; If either x or y is not present , nothing to do ; If x is not head of linked list ; Else make y as new head ; If y is not head of linked list ; Else make x as new head ; Swap next pointers ; Function to add a node at the beginning of List ; allocate node and put in the data ; link the old list off the new node ; move the head to point to the new node ; Function to print nodes in a given linked list ; Driver program to test above function ; The constructed linked list is : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; void swapNodes ( Node * * head_ref , int x , int y ) { if ( x == y ) return ; Node * prevX = NULL , * currX = * head_ref ; while ( currX && currX -> data != x ) { prevX = currX ; currX = currX -> next ; } Node * prevY = NULL , * currY = * head_ref ; while ( currY && currY -> data != y ) { prevY = currY ; currY = currY -> next ; } if ( currX == NULL currY == NULL ) return ; if ( prevX != NULL ) prevX -> next = currY ; else * head_ref = currY ; if ( prevY != NULL ) prevY -> next = currX ; else * head_ref = currX ; Node * temp = currY -> next ; currY -> next = currX -> next ; currX -> next = temp ; } void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( Node * node ) { while ( node != NULL ) { cout << node -> data << " ▁ " ; node = node -> next ; } } int main ( ) { Node * start = NULL ; push ( & start , 7 ) ; push ( & start , 6 ) ; push ( & start , 5 ) ; push ( & start , 4 ) ; push ( & start , 3 ) ; push ( & start , 2 ) ; push ( & start , 1 ) ; cout << " Linked ▁ list ▁ before ▁ calling ▁ swapNodes ( ) ▁ " ; printList ( start ) ; swapNodes ( & start , 4 , 3 ) ; cout << " Linked list after calling swapNodes ( ) " ; printList ( start ) ; return 0 ; }