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);...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
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...
Adding large numbers with linked list Requirement - in C++ - use file for the input...
Adding large numbers with linked list Requirement - in C++ - use file for the input (nums.txt) - (recommended) use only one linked list to hold intermediate answer and final answer. You may use another one to reverse the answer. - store the num reversely in the linked list. For example, the num 123 is stored as 3 (at first node), 2 (at second node) and 1 (at third node) in the linked list. - write a function that performs...
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 ~...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT