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.
//
2 // Doublylinked list with 2 dummy nodes
3 //
4 #pragma once
6 #include <stdexcept>
8 template<typename T>
9 struct Node {
10 T data;
11 Node<T>* next;
12 Node<T>* prev;
13
14 Node() = delete;
15 Node( const T & element ) : data( element ), next( nullptr ), prev( nullptr ) {}
16 };
17 18
19 template<typename T>
20 class DoublyLinkedList {
21 private:
22 Node<T>* head;
23 Node<T>* tail;
24 public:
25 // Constructors
26 DoublyLinkedList();
27 DoublyLinkedList(const DoublyLinkedList&);
28 DoublyLinkedList& operator=(const DoublyLinkedList&); // assignment operator
29 ~DoublyLinkedList(); // destructor
30
31 // Getters / Setters
32 bool empty();
33 int size() = delete; // INTENTIONALLY NOT IMPLEMENTED !!
34
35 void
36 void
37 void
38 void
39
40 void
41 void
42 T&
43 T&
44
45 void
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
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.
You are to write well-formed C++ code that defines and implements a new public member function...