Question
I am trying to run this c++ program I input this: 4 Kale Lettuce Carrots Peanuts The output should be this: Kale Lettuce Carrots Peanuts
I am trying to run this c++ program
I input this:
4
Kale
Lettuce
Carrots
Peanuts
The output should be this:
Kale
Lettuce
Carrots
Peanuts
But instead, it is showing no output is there any way to fix this?
-------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 != NULL) { currNode->PrintNodeData(); currNode = currNode->GetNext(); } }
----ItemNode.h------
#include
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
void InsertAtEnd(ItemNode *&nodeLoc)
{ ItemNode *temp = this;
while (temp->nextNodeRef != NULL) { temp = temp->nextNodeRef; temp->nextNodeRef = nodeLoc;}
}
// 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
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