10.21 LAB: Grocery shopping list (LinkedList) 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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 17SA
icon
Related questions
Question

Starter code for ShoppingList.java

 

import java.util.*;
import java.util.LinkedList;

public class ShoppingList{
    public static void main(String[] args)
    {
        Scanner scnr=new Scanner(System.in);
        LinkedList<ListItem>shoppingList=new LinkedList<ListItem>();//declare LinkedList
        String item;
        int i=0,n=0;//declare variables
        item=scnr.nextLine();//get input from user
        while(item.equals("-1")!=true)//get inputuntil user not enter -1
        {
            shoppingList.add(new ListItem(item));//add into shoppingList LinkedList
            n++;//increment n
            item=scnr.nextLine();//get item from user
        }
        for(i=0;i<n;i++)
        {
            shoppingList.get(i).printNodeData();//call printNodeData()for each object
        }
    }
}

class ListItem{
    String item;
    
    //constructor
    ListItem(String item)
    {
        this.item=item;
    }
    
    void printNodeData()
    {
        System.out.println(item);
    }
}

10.21 LAB: 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:
milk
bread
eggs
waffles
cereal
-1
the output is:
milk
bread
eggs
waffles
cereal
Transcribed Image Text:10.21 LAB: 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: milk bread eggs waffles cereal -1 the output is: milk bread eggs waffles cereal
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Parallel Processing
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
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr