Please do this in C++ Create a random number generator, you will need to create
ID: 3873890 • Letter: P
Question
Please do this in C++
Create a random number generator, you will need to create (4) data sets of 100 random integers each with values of 0 to 99. Additionally, you will need to create (1) data set of 100 random integers values of 1 to 3. The latter is for priority of the insert priority queue. Create your data sets once and write them to a file for later reading in or create them each run.
Insert tail (Last In First Out) (LIFO)
Insert sort (largest to tail) Insert priority (1 at tail then 2, 3)
Create and destroy/delete is needed. Use pointer passing in arguments.
Explanation / Answer
//RandomNumber.cpp
#include<iostream>
#include <fstream>
using namespace std;
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
void InsertSort(struct Node** head_ref, struct Node* new_node)
{
struct Node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next != NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */
/* A utility function to create a new node */
struct Node *newNode(int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
//Insert at tail
void InsertTail(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->data = new_data;
/* 3. This new node is going to be the last node, so make next
of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
return;
}
/* Drier program to test count function*/
int main()
{
/* Start with the empty list */
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* head3 = NULL;
ofstream out;
out.open("output.txt");
//Insert at tail
for (int i = 0; i < 100; i++)
{
int n = rand() % 100;
InsertTail(&head1, n);
}
out << "Created Linked List with Insertion at tail: ";
struct Node *temp = head1;
while (temp != NULL)
{
out << temp->data << " ";
temp = temp->next;
}
//Insert sorted Order
for (int i = 0; i < 100; i++)
{
int n = rand() % 100;
InsertSort(&head2, newNode(n));
}
out << " Created Sorted Linked List: ";
temp = head2;
while (temp != NULL)
{
out << temp->data << " ";
temp = temp->next;
}
//Insert Priority
//Insert sorted Order
for (int i = 0; i < 100; i++)
{
//Create number between 1 to 3
int n = rand() % 3 + 1;
InsertSort(&head3, newNode(n));
}
out << " Created Priority Linked List: ";
temp = head3;
while (temp != NULL)
{
out << temp->data << " ";
temp = temp->next;
}
system("pause");
out.close();
return 0;
}
//output.txt
Created Linked List with Insertion at tail: 41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36 91 4 2 53 92 82 21 16 18 95 47 26 71 38 69 12 67 99 35 94 3 11 22 33 73 64 41 11 53 68 47 44 62 57 37 59 23 41 29 78 16 35 90 42 88 6 40 42 64 48 46 5 90 29 70 50 6 1 93 48 29 23 84 54 56 40 66 76 31 8 44 39 26 23 37 38 18 82 29 41
Created Sorted Linked List: 0 0 2 4 6 6 7 7 7 9 9 10 10 12 13 13 14 15 17 18 20 21 21 21 21 22 24 24 24 27 28 29 30 30 30 31 33 34 36 36 37 37 38 39 41 41 45 45 46 48 48 48 50 50 50 52 53 53 55 55 55 57 58 58 59 61 62 66 67 67 68 68 70 72 73 73 74 74 77 77 81 83 83 83 84 86 86 87 88 88 90 91 91 91 93 95 96 97 99 99
Created Priority Linked List: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3