# Answered! Given a linked list of floats, write a function to compute the average of the list of values…

Given a linked list of floats, write a function to compute the average of the list of values

#include <iostream>
using namespace std;

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Given a linked list of floats, write a function to compute the average of the list of values…
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

// Node class
double data;

public:
void SetNext(LinkedNode* aNext) { next = aNext; };
double Data() { return data; };
LinkedNode* Next() { return next; };
};

// List class
class List {
public:
List() { head = NULL; };
void Print();
void Append(double data);
void Delete(int data);
int sumOfNodes();
};

/**
* Print the contents of the list
*/
void List::Print() {

// Temp pointer

// No nodes
if ( tmp == NULL ) {
cout << “EMPTY” << endl;
return;
}

// One node in the list
if ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << ” –> “;
cout << “NULL” << endl;
}
else {
// Parse and print the list
do {
cout << tmp->Data();
cout << ” –> “;
tmp = tmp->Next();
}
while ( tmp != NULL );

cout << “NULL” << endl;
}
}
int List::sumOfNodes()
{
double sum = 0;
int i=1;
double Avg;
while( currentPtr != NULL)
{
sum = sum + currentPtr->Data();
currentPtr = currentPtr->Next();
Avg=sum/i;
i++;
}
cout<<“The Average is “<<Avg<<endl;
}
/**
* Append a node to the linked list
*/
void List::Append(double data) {

// Create a new node
newNode->SetData(data);
newNode->SetNext(NULL);

// Create a temp pointer

if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}

// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
}
}

int main()
{
// New list
List list;

list.Append(100.55);
list.Print();
list.Append(200.66);
list.Print();

list.sumOfNodes();

}

Output:

100.55 –> NULL

100.55 –> 200.66 –> NULL

The Average is 150.605