Question

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

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

Hello, I searched for similar problems, but their InsertAtEnd() have two parameters, while my question asks for one.

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

//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
void InsertAtEnd(ItemNode *currNode){
ItemNode * headNode;
if(currNode == 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;
   }
};

---------------------------------------------------------------------------------

#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();
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

To meet your requirment I have changed in two place.

1. In main method.

2. In InsertAtEnd method

#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
    void InsertAtEnd(ItemNode *lastNode)
    {
        if (nextNodeRef == NULL)
        {
            nextNodeRef = lastNode;
        }
        else
        {
            ItemNode *secondLast = nextNodeRef;

            while (secondLast->nextNodeRef != NULL)
            {
                secondLast->PrintNodeData();
                secondLast = secondLast->nextNodeRef;
            } // add currnode to last node
            secondLast->nextNodeRef = lastNode;
        }
    }

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

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

int main()
{
    ItemNode *headNode; // Create intNode objects
    ItemNode *currNode;

    string item;
    int i;
    int input;

    cout << "Enter Number of item :";

    cin >> input;

    for (i = 0; i < input; i++)
    {
        cout << "Enter Item : ";
        cin >> item;

        currNode = new ItemNode(item);
        if (i == 0)
        {
            headNode = currNode;
        }
        else
        {
            headNode->InsertAtEnd(currNode);
        }
    }

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

past this code in single file and complie and run.

If you find this answer, useful give thumbs up.

if you have any query, ask in comment.

Regards.

Add a comment
Know the answer?
Add Answer to:
10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Grocery shopping list (linked list: inserting at the end of a list)

    import java.util.Scanner;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      ItemNode headNode;  // Create intNode objects                                                         ItemNode currNode;      ItemNode lastNode;      String item;      int i;      // Front of nodes list           ...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

    THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • Grocery shopping list (LinkedList)

    Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      // TODO: Declare a LinkedList called shoppingList of type ListItem      String...

  • Grocery shopping list (LinkedList)

    Given a ListItem class, complete main() using the built-in LinkedList type to create a linked list called shoppingList. The program should read items from input (ending with -1), adding each item to shoppingList, and output each item in shoppingList using the printNodeData() method.Ex. If the input is:the output is:--ShoppingList.java:import java.util.Scanner;import java.util.LinkedList;public class ShoppingList {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);      // TODO: Declare a LinkedList called shoppingList of type ListItem      String...

  • Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6...

    Modify the code to append the additional numbers: 30, -30, 90, -1, 100, 40, and 6 Implement the Prepend function (hint: think of how you can modify the append function) Prepend 5 to the linked list code must be done in c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; class IntNode { public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodePtr); IntNode* GetNext(); void PrintNodeData(); IntNode* Append(int dataVal); private: int dataVal; IntNode* nextNodePtr; }; //...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • 9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...

    9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT