
- 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
Correct the Random Pointer in Doubly Linked List in C++
In this tutorial, we will be discussing a program to correct the random pointer in a doubly linked list.
For this we will be provided with a doubly linked list with one node having a random pointer. Our task is to rectify the element to whom the pointer should be pointing i.e the element next to it.
Example
#include <bits/stdc++.h> using namespace std; //node structure for doubly linked list struct node { int data; node* next; node* prev; }; //new node creation node* newNode(int data){ node* temp = new node; temp->data = data; temp->next = temp->prev = NULL; return temp; } //correcting the random pointer void get_cpointer(node*& head){ if (!head) return; node* temp = head; if (head->next && head->next->prev != head) { head->next->prev = head; return; } //changing if the position is incorrect if (head->prev != NULL) { head->prev = NULL; return; } temp = temp->next; while (temp) { if (temp->next && temp->next->prev != temp) { temp->next->prev = temp; return; } else if (temp->prev && temp->prev->next != temp) { temp->prev->next = temp; return; } temp = temp->next; } } //printing the doubly linked list void printList(node* head) { node* temp = head; while (temp) { cout << temp->data << " ("; cout << (temp->prev ? temp->prev->data : -1)<< ") "; temp = temp->next; } cout << endl; } int main(){ node* head = newNode(1); head->next = newNode(2); head->next->prev = head; head->next->next = newNode(3); head->next->next->prev = head; head->next->next->next = newNode(4); head->next->next->next->prev = head->next->next; cout << "\nIncorrect Linked List: "; printList(head); get_cpointer(head); cout << "\nCorrected Linked List: "; printList(head); return 0; }
Output
Incorrect Linked List: 1 (-1) 2 (1) 3 (1) 4 (3) Corrected Linked List: 1 (-1) 2 (1) 3 (2) 4 (3)
- Related Articles
- The Doubly Linked List class in Javascript
- Difference between Singly linked list and Doubly linked list in Java
- Doubly Linked List as Circular in Javascript
- Find the largest node in Doubly linked list in C++
- Copy list with random Pointer in C++
- Copy list with random Pointer in Python
- Priority Queue using doubly linked list in C++
- Linked List Random Node in C++
- Delete a node in a Doubly Linked List in C++
- C++ Program to Implement Doubly Linked List
- Creating a Doubly Linked List using Javascript
- Reverse a Doubly Linked List using C++
- Program to find size of Doubly Linked List in C++
- Searching an Element in Doubly Circular Linked List using C++
- Find pairs with given sum in doubly linked list in C++

Advertisements