Question

Machine Problem 3 - Linked List C++ For this assignment you will write a program that...

Machine Problem 3 - Linked List 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.

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

Homework Answers

Answer #1

// LinkedList.h

#ifndef LL_H_INCLUDED
#define LL_H_INCLUDED


/* Link list node */
struct node
{
int data;
struct node* next;
};

void sortedInsert(struct node** head_ref, struct node* new_node);
struct node *newNode(int new_data);
void printList(struct node *head);
void removeDuplicate(struct node **c);
void SortedMerge(struct node* a, struct node *b, struct node **c);
int sum(struct node *head);
double average(struct node *head);
#endif // LL_H_INCLUDED

//LinkedList.cpp


/* Program to insert in a sorted list */
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "LinkedList.h"

using namespace std;


/*
Function to insert node in sorted order
*/
void sortedInsert(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;
}
}

/* 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;
}

/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}

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

/* Advance the source pointer */
*sourceRef = newNode->next;

/* Link the old dest off the new node */
newNode->next = *destRef;

/* Move dest to point to the new node */
*destRef = newNode;
}

/*
Utility function to removed duplicates
*/
void removeDuplicate(struct node **c)
{
struct node *temp = *c;
//printf("%d\n", temp->data);
while(temp != NULL && temp->next != NULL)
{
if (temp->data == temp->next->data)
{
if (temp->next->next != NULL)
{
temp->next = temp->next->next;
}
else
{
temp->next = NULL;
}
}
else
{
temp = temp->next;
}
}
}

/*
Merge two linked list
*/
void SortedMerge(struct node* a, struct node *b, struct node **c)
{
/* a dummy first node to hang the result on */
struct node dummy;

/* tail points to the last result node */
struct node* tail = &dummy;

/* so tail->next is the place to add new nodes
to the result. */
dummy.next = NULL;
while (1)
{
if (a == NULL)
{
/* if either list runs out, use the
other list */
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;
}

*c = dummy.next;
removeDuplicate(c);


}

/* Function to compute sum of linked list */
int sum(struct node *head)
{
int total = 0;
struct node *temp = head;
while(temp != NULL)
{
total += temp->data;
temp = temp->next;
}
}

/* Function to take average of linked list */
double average(struct node *head)
{
double total = 0.0;
int count = 0;
struct node *temp = head;
while(temp != NULL)
{
total += temp->data;
count++;
temp = temp->next;
}

return (total/count);
}

/* Drier program to test function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;

for(int i = 0; i < 20; i++)
{
int randNum = rand()%(101);
struct node *new_node = newNode(randNum);
sortedInsert(&head, new_node);

}

/* Start with the empty list */
struct node* head2 = NULL;

for(int i = 0; i < 15; i++)
{
int randNum = rand()%(101);
struct node *new_node = newNode(randNum);
sortedInsert(&head2, new_node);

}

printf("\nIf the first list has \n");
printList(head);


printf("\nAnd the second list has\n");
printList(head2);

struct node* res = NULL;
SortedMerge(head, head2, &res);

printf("\nThe result will:\n");

printList(res);

printf("The sum of the final list’s elements is : %d", sum(res));
printf("The average of the final list is : %f", average(res));

return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Student Grades Student Test Grade 1 76 62 2 84 90 3 79 68 4 88...
Student Grades Student Test Grade 1 76 62 2 84 90 3 79 68 4 88 84 5 76 58 6 66 79 7 75 73 8 94 93 9 66 65 10 92 86 11 80 53 12 87 83 13 86 49 14 63 72 15 92 87 16 75 89 17 69 81 18 92 94 19 79 78 20 60 71 21 68 84 22 71 74 23 61 74 24 68 54 25 76 97...
Using the accompanying Student Grades​ data, construct a scatter chart for midterm versus final exam grades...
Using the accompanying Student Grades​ data, construct a scatter chart for midterm versus final exam grades and add a linear trendline. What is the​ model? If a student scores 7878 on the​ midterm, what would you predict her grade on the final exam to​ be? Student Midterm Final Exam 1 75 64 2 85 91 3 80 68 4 88 83 5 76 60 6 67 80 7 78 74 8 95 94 9 67 61 10 93 87 11...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
#10.     You are to determine if a new online teaching model is better than the existing...
#10.     You are to determine if a new online teaching model is better than the existing model, labelled old. You administer random tests to a pre-selected group of students first for material taught the old model and then under the new model. The data is given in the Excel file and is labelled problem # 10. Carry out an appropriate procedure in hypothesis testing to determine if the new model is more effective for learning. new old 78 4 86...
Below represent scores on an exam, each entry one score for one student 40 99 59...
Below represent scores on an exam, each entry one score for one student 40 99 59 98 63 63 64 65 67 35 67 67 68 70 71 71 71 46 72 72 60 73 74 74 74 75 97 75 62 76 76 76 76 76 77 57 77 98 77 63 78 78 78 79 79 80 80 80 80 80 81 81 92 81 93 82 82 83 83 83 83 83 83 83 84 84 84...
TestScore 53 53 56 56 56 58 58 58 59 59 59 60 60 62 63...
TestScore 53 53 56 56 56 58 58 58 59 59 59 60 60 62 63 63 63 64 65 65 66 67 67 67 67 68 69 69 69 69 71 71 72 72 72 72 73 73 73 73 73 74 75 75 75 76 76 76 76 77 77 77 77 77 78 78 78 79 79 79 79 80 80 80 80 80 80 81 81 81 82 82 83 83 83 83 84 84 84...
Refer to the accompanying data set and construct a 95​% confidence interval estimate of the mean...
Refer to the accompanying data set and construct a 95​% confidence interval estimate of the mean pulse rate of adult​ females; then do the same for adult males. Compare the results. Males    Females 81           82 72           94 52           57 59           66 51           54 62           80 52           77 74           85 51           89 62           57 73           35 61           64 62           87 80           74 80           79 63           62 63           67 97           76 43           60 85           65 72           86 66           85 73           69 72          ...
10 A) What is the best regression to forecast salary? 10 C) Are  all variables statistically significant?...
10 A) What is the best regression to forecast salary? 10 C) Are  all variables statistically significant? Did you drop any Final Math Pre req Hours Work experience 94 92 5 Y 74 90 3 Y 74 87 4 Y 76 84 3 N 66 87 2 N 80 49 4 Y 74 42 3 N 71 61 4 N 84 81 5 N 76 67 5 Y 95 93 4 N 78 56 5 N 71 54 3 Y 82...
The following scores represent a sample of final examination grades for a statistics course 23 60...
The following scores represent a sample of final examination grades for a statistics course 23 60 98 32 57 74 52 70 82 69 74 63 80 62 80 77 81 95 41 65 92 85 85 61 36 79 55 76 52 10 64 75 78 25 80 48 83 64 88 82 81 67 41 71 67 54 34 72 74 43 60 78 84 89 76 84 17 90 15 79 a) Compute the 20th percentile of...
The data file ExxamScores shows the 40 students in a TOM 3010 course exxam scores for...
The data file ExxamScores shows the 40 students in a TOM 3010 course exxam scores for the Middtermm and Final exxam. Is there statistically significant evidence to show that students score lower on their final exxam than middtermm exxam? Provide the p-value for this analysis.ROUND TO 4 DECIMAL PLACES. ExxamScores Student ID # Middtermmm Final 56065 97 64 79499 95 85 59716 89 72 83504 79 64 77735 78 74 57760 87 93 78204 83 70 81177 94 79 54398...