//------------------------------------------------------------------------- // RefSortedList.java by Dale/Joyce/Weems Chapter 6 // // Implements the ListInterface using a linked list. It is kept in increasing // order as defined by the compareTo method of the added elements. Only // Comparable elements may be added to a list. // // Null elements are not permitted on a list. // // ajg version: implements UNBOUNDEDListInterface, format, while-->for // initialize in declarations so default noarg constructor OK //---------------------------------------------------------------------------- package ch06.lists; import support.LLNode; public class RefSortedList> extends RefUnsortedList implements UnboundedListInterface { // Add element to the list. public void add(T element) { // Essentially doing a find so use the fields used by find // Find first list element >= element to be added (cf. find()) for(location=list; location!=null; location=location.getLink()) { if (location.getInfo().compareTo(element) >= 0) break; previous = location; } LLNode newNode = new LLNode(element); if (location == list) { // Insert as first node newNode.setLink(list); list = newNode; } else { // Insert elsewhere newNode.setLink(location); previous.setLink(newNode); } numElements++; } } // Local Variables: // compile-command: "cd ../..; javac ch06/lists/RefSortedList.java" // End: