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
have a java application need to create an application which is able to do some analysis...
have a java application need to create an application which is able to do some analysis on temperature data stored in a data file. You will be given the “temperatures.dat” data file which contains the data you must analyze. The analysis you’ll need to do is: Total number of data points Find coldest temperature Find warmest temperature Find average temperature Find the frequency of each temperature Find the most frequent temperature Find the least frequent temperature All classes must be...
Consider the below vector x, which you can copy and paste directly into Matlab. The vector...
Consider the below vector x, which you can copy and paste directly into Matlab. The vector contains the final grades for each student in a large linear algebra course. x = [61 52 63 58 66 92 64 55 76 60 70 78 76 73 45 63 97 70 100 76 50 64 42 100 67 81 81 59 68 62 72 99 66 76 81 59 47 84 67 75 63 86 73 44 51 69 48 74 61...
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...
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          ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT