Hello, I'm new here and I would like to know about how to make a linked list with random numbers inside it.
I know how to make the nodes, but I'm confused on how to make the numbers inside the list to be random and different from each other.
Do any of you users have a sample code or a program which I can use as a reference?
Thank you
Use the rand() function.
Remember to seed it first using something like time(NULL):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <time.h> //For time() function
class Linked_List
{
int random_number;
public:
Linked_List() //Stick the random function in the constructor
{
random_number = rand()%100 //Will gen a number between 0-99
}
};
int main()
{
srand(time(NULL)); //Seed rand
return 0;
};
EDIT: When I wrote "class Linked_List" I meant "class Node"
If your list is anything like the standard C++ linked list, it has a push_back() function. In that case, you can use the C++ function generate_n() to populate it: