Question

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                                                                         

      headNode = new ItemNode();

      lastNode = headNode;


      int input = scnr.nextInt();


      for(i = 0; i < input; i++ ){

         item = scnr.next();

         currNode = new ItemNode(item);

         lastNode.insertAtEnd(headNode, currNode);

         lastNode = currNode;

      }


      // Print linked list                                                                           

      currNode = headNode.getNext();

      while (currNode != null) {

         currNode.printNodeData();

         currNode = currNode.getNext();

      }

   }

}




public class ItemNode {

   private String item;

   private ItemNode nextNodeRef; // Reference to the next node                                        


   public ItemNode() {

      item = "";

      nextNodeRef = null;

   }


   // Constructor                                                                                     

   public ItemNode(String itemInit) {

      this.item = itemInit;

      this.nextNodeRef = null;

   }


   // Constructor                                                                                     

   public ItemNode(String itemInit, ItemNode nextLoc) {

      this.item = itemInit;

      this.nextNodeRef = nextLoc;

   }


   // Insert node after this node.                                                                   

   public void insertAfter(ItemNode nodeLoc) {

      ItemNode tmpNext;


      tmpNext = this.nextNodeRef;

      this.nextNodeRef = nodeLoc;

      nodeLoc.nextNodeRef = tmpNext;

   }


   // TODO: Define insertAtEnd() method that inserts a node

   //       to the end of the linked list  



   // Get location pointed by nextNodeRef                                                             

   public ItemNode getNext() {

      return this.nextNodeRef;

   }

   

   public void printNodeData() {

      System.out.println(this.item);

   }

}


0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
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.

Similar Homework Help Questions
  • 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  Peanutswhere 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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; }; //...

  • 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...

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