Question

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                                                                         

      headNode = new InventoryNode();

      lastNode = headNode;


      int input = scnr.nextInt();


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

         item = scnr.next();

         numberOfItems = scnr.nextInt();

         currNode = new InventoryNode(item, numberOfItems);

         currNode.insertAtFront(headNode, currNode);

         lastNode = currNode;

      }


      // Print linked list                                                                   

      currNode = headNode.getNext();

      while (currNode != null) {

         currNode.printNodeData();

         currNode = currNode.getNext();

      }

   }

}




public class InventoryNode {

   private String item; 

   private int numberOfItems;

   private InventoryNode nextNodeRef; // Reference to the next node                                   


   public InventoryNode() {

      item = "";

      numberOfItems = 0;

      nextNodeRef = null;

   }


   // Constructor                                                                                     

   public InventoryNode(String itemInit, int numberOfItemsInit) {

      this.item = itemInit;

      this.numberOfItems = numberOfItemsInit;

      this.nextNodeRef = null;

   }


   // Constructor                                                                                     

   public InventoryNode(String itemInit, int numberOfItemsInit, InventoryNode nextLoc) {

      this.item = itemInit;

      this.numberOfItems = numberOfItemsInit;

      this.nextNodeRef = nextLoc;

   }


   // TODO: Define an insertAtFront() method that inserts a node at the 

   //        front of the linked list (after the dummy head node)

   


   // Get location pointed by nextNodeRef                                                             

   public InventoryNode getNext() {

      return this.nextNodeRef;

   }

   

   // Print node data

   public void printNodeData() {

      System.out.println(this.numberOfItems + " " + 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:
Inventory (linked lists: insert at the front 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
  • 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           ...

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

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

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

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

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

  • 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