
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Point to Next Higher Value Node in a Linked List with an Arbitrary Pointer in C++
In this problem, we are given a linked list with a value, link pointer and an arbitrary pointer. Our task is to make the arbitrary pointer point to point the next large value in the list.
Let’s take an example to understand the problem,
Here, we can see 8 points to 12, 12 to 41, 41 to 54, 54 to 76 which are successive larger elements of the linked list.
To solve this problem, we will use the merge sort algorithm to sort elements and use the sort as a linked list for the arbitrary pointer.
For this we will use the merge sort algorithm on linked list treating the arbitrary pointer as a primary pointer for sorting and linked list, this will solve the problem i.e. each arbitrary point will point to the next larger node then it.
Example
Program to show an implementation of our solution,
#include <iostream> using namespace std; class Node { public: int data; Node* next, *arbit; }; Node* SortedMerge(Node* a, Node* b); void FrontBackSplit(Node* source, Node** frontRef, Node** backRef); void MergeSort(Node** headRef) { Node* head = *headRef; Node* a, *b; if ((head == NULL) || (head->arbit == NULL)) return; FrontBackSplit(head, &a, &b); MergeSort(&a); MergeSort(&b); *headRef = SortedMerge(a, b); } Node* SortedMerge(Node* a, Node* b) { Node* result = NULL; if (a == NULL) return (b); else if (b == NULL) return (a); if (a->data <= b->data){ result = a; result->arbit = SortedMerge(a->arbit, b); } else { result = b; result->arbit = SortedMerge(a, b->arbit); } return (result); } void FrontBackSplit(Node* source, Node** frontRef, Node** backRef) { Node* fast, *slow; if (source == NULL || source->arbit == NULL){ *frontRef = source; *backRef = NULL; return; } slow = source, fast = source->arbit; while (fast != NULL){ fast = fast->arbit; if (fast != NULL){ slow = slow->arbit; fast = fast->arbit; } } *frontRef = source; *backRef = slow->arbit; slow->arbit = NULL; } void addNode(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); new_node->arbit = NULL; (*head_ref) = new_node; } Node* populateArbitraray(Node *head) { Node *temp = head; while (temp != NULL){ temp->arbit = temp->next; temp = temp->next; } MergeSort(&head); return head; } int main() { Node* head = NULL; addNode(&head, 45); addNode(&head, 12); addNode(&head, 87); addNode(&head, 32); Node *ahead = populateArbitraray(head); cout << "\t\tArbitrary pointer overlaoded \n Traversing linked List\n"; cout<<"Using Next Pointer\n"; while (head!=NULL){ cout << head->data << ", "; head = head->next; } printf("\nUsing Arbit Pointer\n"); while (ahead!=NULL){ cout<<ahead->data<<", "; ahead = ahead->arbit; } return 0; }
Output
Arbitrary pointer overlaoded Traversing linked List Using Next Pointer 32, 87, 12, 45, Using Arbit Pointer 12, 32, 45, 87,