Question

You are to write well-formed C++ code that defines and implements a new public member function...

You are to write well-formed C++ code that defines and implements a new public member function of the attached doubly linked list data structure, which is implemented using two dummy nodes.

Implement your function using recursion. That is, your new public member function must call a new private, recursive helper function. A non-recursive version of the helper function will get no credit. The helper function should not have any loops at all. Do not use any global variables. Both new functions should call existing member functions when appropriate. Refer to the attached code for the existing functions available. The attachment is for reference only. Write your response in the space below clearly indicating which is the public and which is the private function.   Click here to view the attachment.

You cannot add additional attributes to the class. You may assume the default and copy constructors, and all the operators (e.g., <, <=, ==, +, ++, =, +=, <<) are defined for element type T. The description of the new public function you are to write is:

T getDifference();

This function takes no arguments and returns the difference of all elements in the linked list, or throws an exception (e.g., std::length_error) if the list is empty. For example, if the list is

    _head → dummy_node ↔ 10 ↔ 23 ↔ 15 ↔ 23 ↔ 15 ↔ dummy_node ← _tail          

then getDifference () returns -66, which is 10 - 23 - 15 - 23 - 15.

Hint: recurse from the tail towards the head.

  1. //

  2. 2 // Doubly­linked list with 2 dummy nodes

  3. 3 //

  4. 4 #pragma once

6 #include <stdexcept>

  1. 8 template<typename T>

  2. 9 struct Node {

  1. 10 T data;

  2. 11 Node<T>* next;

  3. 12 Node<T>* prev;

13

  1. 14 Node() = delete;

  2. 15 Node( const T & element ) : data( element ), next( nullptr ), prev( nullptr ) {}

  3. 16 };

17 18

  1. 19 template<typename T>

  2. 20 class DoublyLinkedList {

  3. 21 private:

  4. 22 Node<T>* head;

  5. 23 Node<T>* tail;

  6. 24 public:

  7. 25 // Constructors

  8. 26 DoublyLinkedList();

  9. 27 DoublyLinkedList(const DoublyLinkedList&);

  10. 28 DoublyLinkedList& operator=(const DoublyLinkedList&); // assignment operator

  11. 29 ~DoublyLinkedList(); // destructor

30

  1. 31 // Getters / Setters

  2. 32 bool empty();

  3. 33 int size() = delete; // INTENTIONALLY NOT IMPLEMENTED !!

34

  1. 35 void

  2. 36 void

  3. 37 void

  4. 38 void

39

  1. 40 void

  2. 41 void

  3. 42 T&

  4. 43 T&

44

  1. 45 void

  2. 46 };

append( const T& );
prepend( const T& );
insertAfter( Node<T>*, const T& ); remove( Node<T>* );

pop_front(); pop_back(); front(); back();

// remove element at front of list // remove element at back of list

clear();

// Intentionally no default constructor

// return list's front element // return list's back element

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
You are to write well-formed C++ code that defines and implements a new public member function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
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