import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Scanner; /** * Implementation of Homework 6. This is a modified LinkedList class. It provide * the add, iterator, and size methods defined in the Collection interface. It * initializes the iterator returned with the head of the list. * * @author Cheng Jade * @assignment ICS211 Homework TA6 * @date Mar 05, 2010 * @bugs None */ public class ChengJade6<T> implements Iterable<T> { // Define the head node. The linked list is empty when the head node is null. private Node<T> myHead = null; private int mySize = 0; /** * The main entry point of the application. * * @param args The command line arguments. */ public static void main(final String[] args) { // Define a linked list collection. final ChengJade6<String> list = new ChengJade6<String>(); // Check there is a path on the command line. if (args.length != 1) { System.err.println("Usage: ChengJade6 path"); System.exit(1); } // Define a FileReader to take in the file. Define a scanner to scan // through the contents and add each line into the linked list. try { final FileReader file = new FileReader(args[0]); final Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) list.add(scanner.nextLine()); } catch (final FileNotFoundException e) { System.err.println("Unable to open the file."); System.exit(1); } // Print out the size of the collection. System.out.println("The size of the collection is: " + list.size() + "\n"); // Define an Iterator object and initialize it with the first item of the // list. final Iterator<String> iterator = list.iterator(); // Loop over and print out the contents of the list using the iterator. while (iterator.hasNext()) { final String item = iterator.next(); System.out.println(item.length() + ": " + item); } // Alternate syntax: // // for (String s : list) // ��System.out.println(s.length() + ": " + s); // Return zero to let the eclipse debugger close happily. System.exit(0); } /** * Returns an iterator that can be used to visit each item in the linked list. * * @return An iterator. */ public Iterator<T> iterator() { return new LinkedListIterator<T>(myHead); } /** * Adds an element into the head of the head of the linked list. * * @param o The value */ public boolean add(final T item) { mySize++; myHead = new Node<T>(item, myHead); return true; } /** * Returns the number of items in the linked list. * * @return The number of items in the linked list. */ public int size() { return mySize; } /** * Returns the string representation of the class data. * * @returns The string representation of the class data. */ @Override public String toString() { // Return a special message for no items in the linked list. if (myHead == null) return "The linked list is empty.\n"; // Loop over each item in the linked list building the string representation // of the linked list nodes values. final StringBuilder builder = new StringBuilder(); for (Node<T> node = myHead; node != null; node = node.next()) builder.append(node + "\n"); return builder.toString(); } } /** * A generic node class. */ class Node<T> { // Define two fields for each node. private Node<T> myNext; private final T myValue; /** * Initializes a new instance of the class. * * @param value The value stored in this node. * @param next A pointer to the next node. */ public Node(final T value, final Node<T> next) { myValue = value; myNext = next; } /** * Returns the pointer to the next node. * * @return The pointer to the next node. */ public Node<T> next() { return myNext; } /** * Returns the value stored in this node. * * @return The value stored in this node. */ public T value() { return myValue; } /** * Sets the pointer to the next node. * * @param next The pointer to the next node. */ public void setNext(final Node<T> next) { myNext = next; } /** * Returns the string representation of the class data. * * @returns The string representation of the class data. */ @Override public String toString() { return myValue == null ? "[null]" : myValue.toString(); } } /** * This is an Iterator class. It provides a hasNext method to check if the next * element is null, a next method to return the current element, and a remove * method, which simply throws an exception. */ class LinkedListIterator<T> implements Iterator<T> { private Node<T> myNode = null; /** * Initializes a new instance of the class. * * @param first The head of the linked list. */ public LinkedListIterator(final Node<T> node) { myNode = node; } /** * This method returns true if there are more items in the collection. * * @return True indicates there are more items in the collection. */ public boolean hasNext() { return myNode != null; } /** * Returns the element, if there is one, or throws an exception if there are * no more items in the collection. * * @return The current item in the collection. * @throws NoSuchElementException Thrown if there are no more items in the * collection. */ public T next() { if (myNode == null) throw new NoSuchElementException(); final T value = myNode.value(); myNode = myNode.next(); return value; } /** * Throws an exception. * * @throws UnsupportedOperationException */ public void remove() { throw new UnsupportedOperationException(); } }