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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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;}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply