C++ Program For Selecting A Random Node From A Singly Linked List

In this article, you will learn how to select a random node from a singly linked list in C++. If you want to select a random node from a singly linked list, you can follow these steps:

Define the Node Structure:

  • Establish a node's structure first in the linked list. Every node should include a data field and a pointer to the following node.
  • Example
    
    struct Node {
    int data;
    Node* next;
    // Constructor to initialize the node
    Node(int value) : data(value), next(nullptr) {}
    };
    

    Create a Singly Linked List:

  • After that, implement a class to represent the singly linked list. This class should have methods for inserting nodes and performing other required tasks.
  • Example
    
    class LinkedList {
    public:
    Node* head;
    // Constructor to initialize the linked list
    LinkedList() : head(nullptr) {}
    // Method to insert a node at the end of the list
    void insertNode(int value) {
    Node* newNode = new Node(value);
    if (!head) {
    head = newNode;
    } else {
    Node* temp = head;
    while (temp->next) {
    temp = temp->next;
    }
    temp->next = newNode;
    }
    }
    };
    

    Selecting a Random Node:

  • After that, you must go through the linked list to choose a random node. Counting the nodes and then creating a random index within that range is one method of accomplishing this. Go through the list once more, stopping at the index that is chosen at random.

Example:

Example

#include <iostream>
#include <cstdlib>
#include <ctime>
struct Node {
int data;
Node* next;
// Constructor to initialize the node
Node(int value) : data(value), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
// Constructor to initialize the linked list
LinkedList() : head(nullptr) {}
// Method to insert a node at the end of the list
void insertNode(int value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Method to select a random node from the list
Node* getRandomNode() {
if (!head) {
return nullptr;  // Return nullptr for an empty list
}
// Count the number of nodes
int count = 0;
Node* temp = head;
while (temp) {
count++;
temp = temp->next;
}
// Generate a random index within the range [0, count-1]
int randomIndex = rand() % count;
// Traverse the list to the randomly selected index
temp = head;
for (int i = 0; i < randomIndex; ++i) {
temp = temp->next;
}
return temp;
}
};
int main() {
srand(time(nullptr));  // Seed for random number generation
LinkedList list;
list.insertNode(1);
list.insertNode(2);
list.insertNode(3);
list.insertNode(4);
list.insertNode(5);
Node* randomNode = list.getRandomNode();
if (randomNode) {
std::cout << "Random Node Value: " << randomNode->data << std::endl;
} else {
std::cout << "The list is empty." << std::endl;
}
return 0;
}

Output:

Output

Random Node Value: 4

Explanation:

Before invoking getRandomNode , this program imports the required headers, initializes the random number generator with srand(time(nullptr)) , and adds a few sample nodes to the linked list in the main function.

Input Required

This code uses input(). Please provide values below: