In c++ Given the IntNode class, define the IndexOf() function to return the index of parameter target or -1 if not found

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 IndexOf() function to return the index of parameter target or -1 if not found

Post by answerhappygod »

In c++
Given the IntNode class, define the IndexOf() function to returnthe index of parameter target or -1 if not found.
Note: The first index after the head node is 0.
Ex: If the list contains:
IndexOf(headNode, 22) returns 2.
Ex: If the list contains:
IndexOf(headNode, 22) returns -1.
#include <iostream>using namespace std;
class IntNode {public:// ConstructorIntNode(int dataInit);
// Get node valueint GetNodeData();
// Get pointer to next nodeIntNode* GetNext();
/* Insert node after this node.Before: this -- nextAfter: 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 -- nextAfter: this -- node -- next*/void IntNode::InsertAfter(IntNode* newNode) {IntNode* tempNext = this->nextNodePtr;this->nextNodePtr = newNode;newNode->nextNodePtr = tempNext;}
// Return index of target itemint IndexOf(IntNode* headNode, int target) {/* Type your code here. */
}
int main() {IntNode* headNode = new IntNode(-1);IntNode* currNode;IntNode* lastNode;
// Initiaize head nodelastNode = headNode;
// Add nodes to the listfor (int i = 0; i < 20; ++i) {currNode = new IntNode(i);lastNode->InsertAfter(currNode);lastNode = currNode;}
cout << IndexOf(headNode, 15) << endl;
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