v. 15 points) implementing data structure operations C++ code for a new public m
ID: 3802679 • Letter: V
Question
v. 15 points) implementing data structure operations C++ code for a new public member function to be added to the doubly linked list data structure used in class. Return the average value of all elements in the list ple a list of integers) Throws a length error exception if the list is empty. template «typename E> E& 0; average You may use loops or recursion as needed. If necessary, you may add private helper function0s) The definition of the DLinkedList class and its related DNode class are shown below along with the space to write your function. Template doubly linked list class using code from Data structures and Algorithms in c++, Goodrich, Tamassia, and Mount, 2011. #pragma once #include class DNode doubly linked list node private: E elem node element value previous node in the list DNode "prev; DNode next next node in the list provide DLinkedList access friend class DLinkedListExplanation / Answer
Here is the average() method for you:
template <typename E>
E& DLinkedList<E>::average()
{
DNode<E> *temp = header;
E sum = 0.0;
int count = 0;
while(temp != nullptr)
{
sum += temp->elem;
temp = temp->next;
count++;
}
return sum / count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.