Question
7. Make a maxNode function which is a recursive function (no credit with a loop). The maxNode function will accept a pointer of a ListNode as a parameter and return the maximum value of the list. The largestNode member function will call the maxNode member function. Your function is No an in - line function, i.e., you should define the destructor in the Implementation cpp file. #ifndef NUMBERLIST_H #define NUMBERLIST_H class NumberList { private: //declare a structure for the list struct ListNode { double value; // the value in this node struct ListNode*next; // to point to the next node }; ListNode *head; // List head pointer public: NumberList() {head = NULL;} ~NumberList(); NumberList(const NumberList &) //linked list operations void appendNode(double); void insertNode(double); void deleteNode(double); void displayList() const; double maxNode() const {return countNodes(head);} }; #endif //Question:
Explanation / Answer
double maxNode(ListNode *head){
if(head->next == NULL)
return head->value;
double Max = maxNode(head->next);
return head->value>Max?Max:head->value;
}