Given a ListItem class, complete main() using the built-in Linked List 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: milk bread eggs waffles cereal -1 the output is: milk bread eggs waffles cereal 1 import java.util.Scanner; 2 import java.util.LinkedList; 3 4 public class ShoppingList 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 28 29 10 11 223995 File is marked as read only 12 public static void main (String[] args) { 1 Scanner scnr = new Scanner(System.in); LinkedListShoppingList = new LinkedList(); 1 public class ListItem { private String item; 14 15 17 String item; String s; do { item= scnr.nextLine(); if(s.length() == 2 && s.charAt(e) == "-" && s.charAt(1) == '1') break; } else } // Print this node 13 public void printNodeData() { ShoppingList.add(new ListItem(s)); } while (true); for (int i = 0; i < ShoppingList.size(); i++) ShoppingList.get(i).printNodeData(0); public ListItem() { item = ""; } public ListItem(String itemInit) { this.item itemInit; Current file: ListItem.java - System.out.println(this.item);

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Java (LinkedList) - Grocery Shopping List

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:
milk
bread
eggs
waffles
cereal
-1
the output is:
milk
bread
eggs
waffles
cereal
1 import java.util.Scanner;
2 import java.util.LinkedList;
3
4 public class ShoppingList
5
10
11
12
13
14
15
16
19
20
21
22
23
24
25
26
27
28
29
3
4
5
10
SEEEEEES
11
12
File is marked as read only
13
1 public class ListItem {
private String item;
14
15
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
LinkedList<ListItem>ShoppingList = new LinkedList<ListItem>();
16
17
String item;
String s;
}
do {
item= scnr.nextLine();
if(s.length() == 2 && s.charAt(0) == "-" && s.charAt(1) == '1')
break;
}
else
ShoppingList.add(new ListItem(s));
}
} while (true);
for (int i = 0; i < ShoppingList.size(); i++)
ShoppingList.get(i).printNodeData(0);
public ListItem() {
item= "";
public ListItem(String itemInit) {
this.item=itemInit;
// Print this node
public void printNodeData() {
Current file: ListItem.java →
System.out.println(this.item);
Transcribed Image Text: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: milk bread eggs waffles cereal -1 the output is: milk bread eggs waffles cereal 1 import java.util.Scanner; 2 import java.util.LinkedList; 3 4 public class ShoppingList 5 10 11 12 13 14 15 16 19 20 21 22 23 24 25 26 27 28 29 3 4 5 10 SEEEEEES 11 12 File is marked as read only 13 1 public class ListItem { private String item; 14 15 public static void main (String[] args) { Scanner scnr = new Scanner(System.in); LinkedList<ListItem>ShoppingList = new LinkedList<ListItem>(); 16 17 String item; String s; } do { item= scnr.nextLine(); if(s.length() == 2 && s.charAt(0) == "-" && s.charAt(1) == '1') break; } else ShoppingList.add(new ListItem(s)); } } while (true); for (int i = 0; i < ShoppingList.size(); i++) ShoppingList.get(i).printNodeData(0); public ListItem() { item= ""; public ListItem(String itemInit) { this.item=itemInit; // Print this node public void printNodeData() { Current file: ListItem.java → System.out.println(this.item);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning