In c++ Given the IntNode class, define the IndexOf() function to return the index of parameter target or -1 if not found
Posted: Thu Jul 14, 2022 2:17 pm
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;}
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;}