Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ON C++ LANGUAGE PLEASE Machine Problem 3 - Linked List For this assignment you w

ID: 3788553 • Letter: O

Question

ON C++ LANGUAGE PLEASE

Machine Problem 3 - Linked List

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.

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


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void MoveNode(struct node** destRef, struct node** sourceRef);

struct node* SortedMerge(struct node* a, struct node* b)
{
struct node dummy;

struct node* tail = &dummy;

dummy.next = NULL;
while (1)
{
if (a == NULL)
{
tail->next = b;
break;
}
else if (b == NULL)
{
tail->next = a;
break;
}
if (a->data <= b->data)
MoveNode(&(tail->next), &a);
else
MoveNode(&(tail->next), &b);

tail = tail->next;
}
return(dummy.next);
}

void MoveNode(struct node** destRef, struct node** sourceRef)
{
struct node* newNode = *sourceRef;
assert(newNode != NULL);

*sourceRef = newNode->next;

newNode->next = *destRef;

*destRef = newNode;
}


void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;
}

void printList(struct node *node)
{
while (node!=NULL)
{
cout<<node->data <<" ";
node = node->next;
}
}

int main()
{
struct node* res = NULL;
struct node* a = NULL;
struct node* b = NULL;
   int value=0;

cout<<"Enter 20 values for the list 1";
   for(int i=0;i<20;i++){
       cin >> value;
       push(&a, value);
   }
   cout<<"Enter 15 values for the list 2";
for(int i=0;i<15;i++){
       cin >> value;
       push(&b, value);
   }

res = SortedMerge(a, b);
   cout<<"Merged Linked List is: ";
printList(res);
  
   struct node* temp = res;
   int sum=0;
   int noofnodes =0;  
   while(temp!=null){
           sum = sum+temp->data;
           noofnodes++;
   }
   cout<<"Total sum of the list is :"<<sum<<" ";
   float average = sum/noofnodes;
   cout<<"Average of the list is :"<<average;
  
return 0;
}