
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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,
- Related Articles
- Point arbit pointer to greatest value right side node in a linked list in C++
- Delete a Node from linked list without head pointer in C++
- C Program to reverse each node value in Singly Linked List
- Linked List Random Node in C++
- C# program to find node in Linked List
- Find modular node in a linked list in C++
- Golang Program to update the first node value in a linked list.
- Golang Program to update the last node value in a linked list.
- C# Program to add a node after the given node in a Linked List
- C# Program to add a node before the given node in a Linked List
- Delete a node in a Doubly Linked List in C++
- Write a function to get Nth node in a Linked List in C++
- Correct the Random Pointer in Doubly Linked List in C++
- Delete Node in a Linked List in Python
- Replace each node with its Surpasser Count in Linked List Using C++
