Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C + + please 1 0 . 2 4 CIST 2 3 6 2 LAB: Grocery shopping list ( linked list: inserting at the

In C++ please
10.24 CIST2362 LAB: Grocery shopping list (linked list: inserting at the end of a list)
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.
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
Here is the code that you can modify:
ItemNode.h:
#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;
}
};
The following code cannot be modified:
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 != NULL){
currNode->PrintNodeData();
currNode = currNode->GetNext();
}
}P

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2 What can organisations do to improve employee utilisation?

Answered: 1 week ago

Question

4 When is it a good idea to use the external supply of labour?

Answered: 1 week ago

Question

3. What would you do now if you were Mel Fisher?

Answered: 1 week ago