/****************************************************************************** * Name: Kevin Wayne * NetID: wayne * Precept: P99 * * Compilation: javac LinkedBag.java * Execution: java LinkedBag < input.txt * * Description: A generic bag or multiset, implemented using a singly linked list. ******************************************************************************/ import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedBag implements Iterable { private Node first; // first node in linked list private int n; // number of elements in bag // helper linked-list class private class Node { private Item item; private Node next; } /** * Initializes an empty bag. */ public LinkedBag() { first = null; n = 0; } /** * Is this bag empty? * @return true if this bag is empty; false otherwise */ public boolean isEmpty() { return first == null; } /** * Returns the number of items in this bag. * @return the number of items in this bag */ public int size() { return n; } /** * Adds the item to this bag. * @param item the item to add to this bag */ public void add(Item item) { Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; n++; } /** * Returns an iterator that iterates over the items in the bag. */ public Iterator iterator() { // add code here } // an iterator over a linked list private class ListIterator implements ____________<_____> { private Node current; // creates a new iterator public ListIterator() { // add code here } // is there a next item in the iterator? public boolean hasNext() { // add code here } // this method is optional in Iterator interface public void remove() { throw new UnsupportedOperationException(); } // returns the next item in the iterator (and advances the iterator) public Item next() { if (!hasNext()) throw new NoSuchElementException(); // add code here } } /** * Unit tests the {@code LinkedBag} data type. * * @param args the command-line arguments */ public static void main(String[] args) { LinkedBag bag = new LinkedBag(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); bag.add(item); } StdOut.println("size of bag = " + bag.size()); for (String s : bag) { StdOut.println(s); } } }