Question: Given main ( ) , define an InsertAtEnd ( ) member function in the ItemNode class that adds an element to the end of a

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 head node that does not contain user-input values.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 head node that does not contain user-input values.
Ex. if the input is:
4
Kale
Lettuce
Carrots
Peanuts
where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list.
The output is:
Kale
Lettuce
Carrots
Peanuts
546298.3504868.qx3zqy7
LAB
ACTIVITY
8.19.1: LAB: Grocery shopping list (linked list: inserting at the end of a list)
010
Ex. if the input is:
4
Kale
Lettuce
Carrots
Peanuts
where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list.
The output is:
Kale
Lettuce
Carrots
Peanuts
-*GIVIN CODE*-
*main.cpp*
#include "ItemNode.h"
int main(){
ItemNode *headNode; // Create intNode objects
ItemNode *currNode;
ItemNode *lastNode;
string item;
int i;
int input;
// Front of nodes list
headNode = new ItemNode();
lastNode = headNode;
cin >> input;
for (i =0; i input; i++){
cin >> item;
currNode = new ItemNode(item);
lastNode->InsertAtEnd(currNode);
lastNode = currNode;
}
// Print linked list
currNode = headNode->GetNext();
while (currNode != nullptr){
currNode->PrintNodeData();
currNode = currNode->GetNext();
}
}
-*ItemNode.h*-
#ifndef MILEAGETRACKERNODEH
#define MILEAGETRACKERNODEH
#include
#include
using namespace std;
class ItemNode {
private:
string item;
ItemNode* nextNodeRef;
public:
// Constructor
ItemNode();
// Constructor
ItemNode(string itemInit);
// Constructor
ItemNode(string itemInit, ItemNode *nextLoc);
// Insert node after this node.
void InsertAfter(ItemNode &nodeLoc);
// TODO: Declare the InsertAtEnd() member function that inserts a parameter ItemNode
// to the end of the linked list
// Get location pointed by nextNodeRef
ItemNode* GetNext();
// Print data of this node
void PrintNodeData();
// Get data of this node
string GetNodeItem();
};
#endif
-*ItemNode.cpp*-
#include "ItemNode.h"
ItemNode::ItemNode(std::string itemName){
item = itemName;
next = nullptr;
}
void ItemNode::InsertAtEnd(std::string itemName){
ItemNode* newNode = new ItemNode(itemName);
ItemNode* current = this;
// Traverse to the last node of the linked list
while (current->next != nullptr){
current = current->next;
}
// Add the new node to the end of the linked list
current->next = newNode;
}
 Given main(), define an InsertAtEnd() member function in the ItemNode class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!