/** * ListTraverse.java * Shows how to call methods from the CS10LinkedList interface * to traverse a linked list. * * @author Tom Cormen */ public class ListTraverse { public static void main(String[] args) { CS10LinkedList myList = new SentinelDLL(); // Make the list contain 1 through 8, in order. for (int i = 1; i <= 8; i++) myList.addLast(i); // Now traverse the list to print it out and sum it up. int sum = 0; for (Integer i = myList.getFirst(); i != null; i = myList.next()) { System.out.println("List element with value " + i); sum += i; } System.out.println("The list elements sum to " + sum); // Another way to traverse the list. sum = 0; for (myList.getFirst(); myList.hasCurrent(); myList.next()) { Integer i = myList.get(); System.out.println("List element with value " + i); sum += i; } System.out.println("The list elements sum to " + sum); } }