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 <stdexcept using namespace std; template typename E class DLinkedList; forward declaration to be used when declaring DNode template ktypename E> class DNode doubly linked list node private: E elem node element value previous node in the list DNode<E> prev; DNode<E> next next node in the list provide DLinkedList access friend class DLinkedList<E template <typename E a doubly linked list class DLinkedList public: empty list constructor DLinked List(); destructor ~D LinkedList()i is list empty? bool empty() const; get front element E& front
media%2F933%2F9334d857-83df-4423-92d0-4f
media%2F9bf%2F9bfba05f-7be3-4683-8c53-93
media%2F2e8%2F2e87e89a-cfd9-4ae4-8ce2-9c
media%2Ff2d%2Ff2d803e6-10fe-4197-a10f-15
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
v. 15 points) implementing data structure operations C++ code for a new public member function to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer in Java. The code that needs to be modified is below. Thank you. Question:...

    Please answer in Java. The code that needs to be modified is below. Thank you. Question: Implement Doubly Linked List add method which add an element to a specific position. - It’s an instance method that takes a position and an element, then adds the element to this specific position and shifts the element currently at this position and any subsequent elements to the right. It throws an exception if the position is out of bound. It traverses the list...

  • Using C++ • Implement the following in both string linked list and in the template version...

    Using C++ • Implement the following in both string linked list and in the template version of the class: • Add to the back and remove from back of the list : - void addBack(….); - string removeBack(); E removeBack(…); • Insert elements in a sorted list i.e., find the position where an element fits and add it to the list. Note you must call the addBack and addFront functions if you insert in the front or rare of the...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...

    Using Java coding, complete the following: This program should implement a LinkedList data structure to handle the management of student records. It will need to be able to store any number of students, using a Linked List. LinearNode.java can be used to aid creating this program Student should be a class defined with the following:    • String name    • int year    • A linked list of college classes (college classes are represented using strings)    • An...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

    Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty. A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function. For example, checkRecurseHelper() should return true for the linked list shown below. A main function (prob3.cpp) is given to you to add data...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT