Question: Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print
Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node.
Here's the code for it:
#include
#include
using namespace std;
class ItemNode {
private:
string item;
ItemNode* nextNodeRef;
public:
// Constructor
ItemNode() {
item = "";
nextNodeRef = NULL;
}
// Constructor
ItemNode(string itemInit) {
this->item = itemInit;
this->nextNodeRef = NULL;
}
// Constructor
ItemNode(string itemInit, ItemNode *nextLoc) {
this->item = itemInit;
this->nextNodeRef = nextLoc;
}
// Insert node after this node.
void InsertAfter(ItemNode &nodeLoc) {
ItemNode* tmpNext;
tmpNext = this->nextNodeRef;
this->nextNodeRef = &nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// TODO: Define InsertAtEnd() function that inserts a node
// to the end of the linked list
// Get location pointed by nextNodeRef
ItemNode* GetNext() {
return this->nextNodeRef;
}
void PrintNodeData() {
cout << this->item << endl;
}
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
