Point arbit pointer to greatest value right side node in a linked list | C ++ program to point arbit pointers to highest value on its right ; Link list node ; This function populates arbit pointer in every node to the greatest value to its right . ; using static maxNode to keep track of maximum orbit node address on right side ; if head is null simply return the list ; if head -> next is null it means we reached at the last node just update the max and maxNode ; Calling the populateArbit to the next node ; updating the arbit node of the current node with the maximum value on the right side ; if current Node value id greater then the previous right node then update it ; Utility function to print result linked list ; Function to create a new node with given data ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next , * arbit ; } ; void populateArbit ( Node * head ) { static Node * maxNode ; if ( head == NULL ) return ; if ( head -> next == NULL ) { maxNode = head ; return ; } populateArbit ( head -> next ) ; head -> arbit = maxNode ; if ( head -> data > maxNode -> data ) maxNode = head ; return ; } void printNextArbitPointers ( Node * node ) { printf ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer STRNEWLINE " ) ; while ( node != NULL ) { cout << node -> data << " TABSYMBOL TABSYMBOL " ; if ( node -> next ) cout << node -> next -> data << " TABSYMBOL TABSYMBOL " ; else cout << " NULL " << " TABSYMBOL TABSYMBOL " ; if ( node -> arbit ) cout << node -> arbit -> data ; else cout << " NULL " ; cout << endl ; node = node -> next ; } } Node * newNode ( int data ) { Node * new_node = new Node ; new_node -> data = data ; new_node -> next = NULL ; return new_node ; } int main ( ) { Node * head = newNode ( 5 ) ; head -> next = newNode ( 10 ) ; head -> next -> next = newNode ( 2 ) ; head -> next -> next -> next = newNode ( 3 ) ; populateArbit ( head ) ; printf ( " Resultant ▁ Linked ▁ List ▁ is : ▁ STRNEWLINE " ) ; printNextArbitPointers ( head ) ; return 0 ; }