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 userinput 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 userinput values.
Ex if the input is:
Kale
Lettuce
Carrots
Peanuts
where 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
qxzqy
LAB
ACTIVITY
: LAB: Grocery shopping list linked list: inserting at the end of a list
Ex if the input is:
Kale
Lettuce
Carrots
Peanuts
where 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
maincpp
#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 ; i input; i
cin item;
currNode new ItemNodeitem;
lastNodeInsertAtEndcurrNode;
lastNode currNode;
Print linked list
currNode headNodeGetNext;
while currNode nullptr
currNodePrintNodeData;
currNode currNodeGetNext;
ItemNodeh
#ifndef MILEAGETRACKERNODEH
#define MILEAGETRACKERNODEH
#include
#include
using namespace std;
class ItemNode
private:
string item;
ItemNode nextNodeRef;
public:
Constructor
ItemNode;
Constructor
ItemNodestring itemInit;
Constructor
ItemNodestring itemInit, ItemNode nextLoc;
Insert node after this node.
void InsertAfterItemNode &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
ItemNodecpp
#include "ItemNode.h
ItemNode::ItemNodestd::string itemName
item itemName;
next nullptr;
void ItemNode::InsertAtEndstd::string itemName
ItemNode newNode new ItemNodeitemName;
ItemNode current this;
Traverse to the last node of the linked list
while currentnext nullptr
current currentnext;
Add the new node to the end of the linked list
currentnext newNode;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
