Page 1 of 1

in c++ Given the IntNode class, define the FindMax() function to return the largest value in the list or -99 if the list

Posted: Thu Jul 14, 2022 2:17 pm
by answerhappygod
in c++
Given the IntNode class, define the FindMax() function to returnthe largest value in the list or -99 if the list is empty. Assumeall values in the list are non-negative.
Ex: If the list contains:
FindMax(headNode) returns 191.
Ex: If the list contains:
FindMax(headNode) returns -99.
#include <iostream>using namespace std;
class IntNode {public: // Constructor IntNode(int dataInit);
// Get node value int GetNodeData();
// Get pointer to next node IntNode* GetNext();
/* Insert node after this node. Before: this -- next After: this -- node -- next */ void InsertAfter(IntNode* newNode);
private: int dataVal; IntNode* nextNodePtr;};
// ConstructorIntNode::IntNode(int dataInit) { this->dataVal = dataInit; nextNodePtr = nullptr;}
// Get node valueint IntNode::GetNodeData() { return this->dataVal;}
// Get pointer to next nodeIntNode* IntNode::GetNext() { return this->nextNodePtr;}
/* Insert node after this node. Before: this -- next After: this -- node -- next*/void IntNode::InsertAfter(IntNode* newNode) { IntNode* tempNext = this->nextNodePtr; this->nextNodePtr = newNode; newNode->nextNodePtr = tempNext;}
// Return largest value in the listint FindMax(IntNode* headNode) {
/* Type your code here. */
}
int main() { IntNode* headNode = new IntNode(-1); IntNode* currNode; IntNode* lastNode; int max;
// Initiaize head node lastNode = headNode;
// Add nodes to the list for (int i = 0; i < 20; ++i) { currNode = new IntNode(i); lastNode->InsertAfter(currNode); lastNode = currNode; }
max = FindMax(headNode); cout << "Max is " << max;
return 0;}