Question

8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...

8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list)

PLEASE ANSWER IN C++

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 

THE FOLLOWING CODE IS GIVEN: "main.pp" & "ItemNode.h"

*Note the main.cpp file 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(headNode, currNode);
       lastNode = currNode;
   }

   // Print linked list   
   currNode = headNode->GetNext();
   while (currNode != NULL) {
       currNode->PrintNodeData();
       currNode = currNode->GetNext();
   }
}

----------ItemNode.h----------

#include <iostream>
#include <string>
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;
   }
};

Please answer this in C++!

Homework Answers

Answer #1

/* 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(headNode, currNode);
lastNode = currNode;
}

// Print linked list   
currNode = headNode->GetNext();
while (currNode != NULL) {
currNode->PrintNodeData();
currNode = currNode->GetNext();
}
}

/*ItemNode.h */

#include <iostream>
#include <string>
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;
}
  
  

   void InsertAtEnd(ItemNode *headNode,ItemNode *currNode){
   if(headNode == NULL){ // check if it is the first Node
   headNode = currNode; // add curr to head
   }else{ // find last node
           while(headNode->nextNodeRef!=NULL){
               headNode = headNode->nextNodeRef;
           }// add currnode to last node
           headNode->nextNodeRef = currNode;
   }
   }

// Get location pointed by nextNodeRef   
ItemNode* GetNext() {
return this->nextNodeRef;
}

void PrintNodeData() {
cout << this->item << endl;
}
};

/* OUTPUT */

/* PLEASE UPVOTE */

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...