Using C++: For this assignment you will write a program that inserts 20 random i
ID: 3791375 • Letter: U
Question
Using C++:
For this assignment you will write a program that inserts 20 random integers from 0 to 100 in order in a linked list object. The program will create another linked list, but with 15 random integers from 0 – 100 in order. The program then will merge those two ordered linked list into a single ordered list. Need to use srand in order to get the random numbers.
The function merge should receive references to each of the list objects to be merged and a reference to a list object into which the merged elements will be placed. There should be no duplicate numbers in the final list.
Calculate the sum of the elements and the floating-point average of the elements.
Don’t use the STL linked list, you need to build your own linked list. You may use the one in the lecture’s example.
An example of the output:
If the first list has
10, 22, 34, 45, 48, 55, 56, 57, 57, 69, 70, 72, 74, 74, 80, 83, 84, 85, 88, 88
And the second list has
50, 55, 57, 79, 81, 84, 87, 88, 90, 92, 95, 95, 95, 96, 99
The result will:
10, 22, 34, 45, 48, 50, 55, 56, 57, 69, 70, 72, 74, 79, 80, 81, 83, 84, 85, 87, 88, 90, 92, 95, 96, 99
The sum of the final list’s elements is : xxxxx
The average of the final list is : xxxx.xx
Please upload the following:
The class .cpp file
The main program
The class .h file
Output File
Explanation / Answer
//linkedlist.cpp
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<assert.h>
using namespace std;
void SwapNode(struct node** destList, struct node** sourceList);
bool SearchData(struct node** destList, struct node* value);
// A nexted list node
struct node
{
int data;
struct node* next;
}*start;
//-- This function used to store the data from the begining of the list
void push(struct node** headReference, int newData)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = (*headReference);
(*headReference) = newNode;
}
//-- Print linked list if it is not null
void printLinkedList(struct node *head)
{
struct node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
struct node* MergeNode(struct node* first, struct node* second)
{
struct node dummy; bool b_allow_merge=true;
struct node* tail = &dummy;
dummy.next = NULL;
while (1)
{
if (first == NULL)
{
tail->next = second;
break;
}
else if (second == NULL)
{
tail->next = first;
break;
}
if (first->data <= second->data)
{
b_allow_merge = SearchData(&tail,first);
SwapNode(&(tail->next), &first);
}
else{
b_allow_merge = SearchData(&tail,second);
SwapNode(&(tail->next), &second);
}
if (b_allow_merge)
tail = tail->next;
}
return(dummy.next);
}
void SwapNode(struct node** destList, struct node** sourceList)
{
struct node* newNode = *sourceList;
assert(newNode != NULL);
*sourceList = newNode->next;
newNode->next = *destList;
*destList = newNode;
}
bool SearchData(struct node** sourceList, struct node* value)
{
bool flag = true;
struct node* newNode = *sourceList;
assert(newNode != NULL);
while (newNode != NULL)
{
if (newNode->data == value->data)
{
flag = false;
}
newNode = newNode->next;
}
return flag;
}
int main()
{
struct node* result = NULL;
struct node* first = NULL;
struct node* second = NULL;
int firstListSize = 20;
int secondListSize = 15;
srand ( time(NULL) );
cout << "The First list is: ";
for (int i=1; i<=firstListSize; i++)
{
int firstList = rand() % 100 + 0;
cout << firstList;
if (i < firstListSize) cout << ",";
push(&first, firstList);
}
cout << " The Second List is: ";
for (int i=1; i<=secondListSize; i++)
{
int secodList = rand() % 100 + 0;
cout << secodList;
if (i < secondListSize) cout << ",";
push(&second, secodList);
}
result = MergeNode(first, second);
cout << " The result of Merged Linked List is: ";
printLinkedList(result);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.