import java.util.Scanner; import java.util.Iterator; /** * Tests the SentinelDLLIterator class. * * @author THC of test program for SentinelDLL * @author Scot Drysdale modified to test SentinelDLLInterator */ public class ListTestIterator { public static void main(String args[]) { CS10IteratedList theList = new SentinelDLLIterator(); // a list to play with, initially empty char command = ' '; // a command String name; // a name CS10ListIterator iter = theList.listIterator(); // an iterator for the list Scanner input = new Scanner(System.in); while (command != 'q') { System.out.print("Command (q, a, n, h, p, H, e, c, s, r, P, R, N, l, f, C, ?): "); command = input.nextLine().charAt(0);; switch (command) { case 'q': // Quit System.out.println("Bye"); break; case 'a': // Add System.out.print("Enter name: "); name = input.nextLine(); iter.add(name); break; case 'n': // next System.out.println(iter.next()); break; case 'h': // hasNext System.out.println(iter.hasNext()); break; case 'p': // previous System.out.println(iter.previous()); break; case 'H': // hasPrevious System.out.println(iter.hasPrevious()); break; case 'e': // isEmpty System.out.println(theList.isEmpty()); break; case 'c': // Contains System.out.print("Enter name: "); name = input.nextLine(); if (theList.contains(name)) System.out.println("Found " + name); else System.out.println("Didn't find " + name); break; case 's': // Set System.out.print("Enter name: "); name = input.nextLine(); iter.set(name); break; case 'r': // Remove iter.remove(); break; case 'P': // Print if (theList.isEmpty()) System.out.println("List is empty"); else System.out.print(theList); break; case 'R': // Reverse Print if (theList.isEmpty()) System.out.println("List is empty"); else { CS10ListIterator printItr = theList.listIterator(); // Go to end of list while(printItr.hasNext()) printItr.next(); while(printItr.hasPrevious()) System.out.println(printItr.previous()); } break; case 'N': // Nested print Iterator outer = theList.listIterator(); while (outer.hasNext()) { System.out.print(outer.next() + ": "); Iterator inner = theList.listIterator(); while (inner.hasNext()) System.out.print(inner.next() + " "); System.out.println(); } break; case 'l': // addLast System.out.print("Enter name: "); name = input.nextLine(); theList.addLast(name); break; case 'f': // addFirst System.out.print("Enter name: "); name = input.nextLine(); theList.addFirst(name); break; case 'C': // Clear iter = theList.listIterator(); while (iter.hasNext()) { iter.next(); iter.remove(); } break; case '?': // Print all the commands System.out.println("Commands are\n q: quit\n n: next\n h: hasNext\n " + "p: previous\n H: hasPrevious\n e: isEmpty\n a: add\n c: contains\n " + "s: set\n r: remove\n P: print\n R: reverse print\n N: nested print\n " + "l: addLast\n f: addFirst\n C: clear\n ?: print this command list\n"); break; default: System.out.println("Huh?"); } } } }