Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question. Implement the following Bag Item > by using a LinkedList, i.e., set up an instance variable of type List Item > , fifill it

Question.

Implement the following BagItem> by using a LinkedList, i.e., set up an instance variable of type ListItem>, fifill it in with a LinkedListItem>at the time of the Bags creation, and use it to store new elements as they come in. Note that the LinkedList can provide a working iterator, so you dont need a private class for the iterator, or the Node class either. Your iterator from LinkedList will have a working remove(), unlike the books iterator, but thats OK.

image text in transcribed Write a client program, BagTest, that puts apple, pear, and apple in a Bag, and then counts the apples in the bag. What methods are available on the Bag object in this client program? Please show Bag.java, TestBag.java, and the answer to the question.

ALGORITHM 1.4 Bag import java.util.Iterator; public class Bag implements Iterable // first node in list private Node first; private class Node Item item; Node next; public void add (Item item) { // same as push() in Stack Node oldfirst = first; first = new Node; first.item = item; first.next = oldfirst; public Iterator iterator { return new ListIterator; } private class ListIterator implements Iterator private Node current = first; public boolean hasNext { return current != null; } public void remove { } public Item next Item item = current.item; current = current.next; return item; This Bag implementation maintains a linked list of the items provided in calls to add(). Code for isEmpty and size is the same as in Stack and is omitted. The iterator traverses the list, main- taining the current node in current. We can make Stack and Queue iterable by adding the code highlighted in red to ALGORITHMS 1.1 and 1.2, because they use the same underlying data structure and Stack and Queue maintain the list in LIFO and FIFO order, respectively. ALGORITHM 1.4 Bag import java.util.Iterator; public class Bag implements Iterable // first node in list private Node first; private class Node Item item; Node next; public void add (Item item) { // same as push() in Stack Node oldfirst = first; first = new Node; first.item = item; first.next = oldfirst; public Iterator iterator { return new ListIterator; } private class ListIterator implements Iterator private Node current = first; public boolean hasNext { return current != null; } public void remove { } public Item next Item item = current.item; current = current.next; return item; This Bag implementation maintains a linked list of the items provided in calls to add(). Code for isEmpty and size is the same as in Stack and is omitted. The iterator traverses the list, main- taining the current node in current. We can make Stack and Queue iterable by adding the code highlighted in red to ALGORITHMS 1.1 and 1.2, because they use the same underlying data structure and Stack and Queue maintain the list in LIFO and FIFO order, respectively

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions