Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6 Implement the Prepend function (hint: think of how you
Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6
Implement the Prepend function (hint: think of how you can modify the append function)
Prepend 5 to the linked list
code must be done in c++
#include#include #include using namespace std; class IntNode { public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodePtr); IntNode* GetNext(); void PrintNodeData(); IntNode* Append(int dataVal); private: int dataVal; IntNode* nextNodePtr; }; // Constructor IntNode::IntNode(int dataInit, IntNode* nextLoc) { this->dataVal = dataInit; this->nextNodePtr = nextLoc; return; } void IntNode::InsertAfter(IntNode* nodeLoc) { IntNode* tmpNext = nullptr; tmpNext = this->nextNodePtr; // Remember next this->nextNodePtr = nodeLoc; // this -- node -- ? nodeLoc->nextNodePtr = tmpNext; // this -- node -- next return; } // Print dataVal void IntNode::PrintNodeData() { cout << this->dataVal << endl; return; } // Grab location pointed by nextNodePtr IntNode* IntNode::GetNext() { return this->nextNodePtr; } IntNode* IntNode::Append(int dataVal) //COMMENT EACH LINE BELOW { IntNode* head; IntNode *last = new IntNode; last->dataVal = dataVal; last->nextNodePtr = NULL; if (head == NULL) { head = last; } else { IntNode *temp = new IntNode; temp = head; while (temp->nextNodePtr != NULL) { temp = temp->nextNodePtr; } temp->nextNodePtr = last; } return head; } int main() { srand(time(0)); IntNode* headObj = nullptr; // Create intNode objects IntNode* nodeObj1 = nullptr; IntNode* nodeObj2 = nullptr; IntNode* nodeObj3 = nullptr; IntNode* nodeObj4 = nullptr; IntNode* nodeObj5 = nullptr; IntNode* nodeObj6 = nullptr; IntNode* nodeObj7 = nullptr; IntNode* nodeObj8 = nullptr; IntNode* nodeObj9 = nullptr; IntNode* nodeObj10 = nullptr; IntNode* currObj = nullptr; int i; IntNode test; // Front of nodes list headObj = new IntNode((rand() % 51) + 50); for(i = 0; i < 9; i++){ nodeObj1 = new IntNode((rand() % 51) + 50); headObj->InsertAfter(nodeObj1); } test.Append(16); // Print linked list currObj = headObj; while (currObj != nullptr) { currObj->PrintNodeData(); currObj = currObj->GetNext(); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started