// The Science of Computing OrderedList class. // History: // August 2003 -- Created by Greg Scragg. // October 2003 -- updates to several methods by GWS // Februray 2004 -- aligned with text, GWS // May 2004 -- Documentation cleaned up by Doug Baldwin. package geneseo.cs.sc; /** * Represents lists whose contents are ordered according to a "less * than" relation. Specifically, the smallest item is first in the list, then * the next smallest, and so forth. Some of this class's methods are destructive, * and therefore do not require assignments within client code when modifying ordered * lists. *

This class was created to support the text Algorithms & Data Structures: * The Science of Computing by Doug Baldwin and Greg Scragg. All references in * this documentation to "the text" refer to that book. In the text, lists * and ordered lists are described in Chapter 11; this class is basically the one * developed in section 11.5.3.

*/ public class OrderedList extends List { /** * Initialize an ordered list to be empty. For example, *

OrderedList oList = new OrderedList();

*/ public OrderedList() { super(); } /** * Create a new list that is an instance of OrderedList, * as required of any subclass of List. Clients of * OrderedList generally do not send this message themselves, * but code inside inherited methods relies on it. * @return A new, empty, OrderedList object */ public List makeNewList() { return new OrderedList(); } /** * Inserts an object into its proper position in an ordered list. Specifically, * the new object is placed after all elements already in the list that are less * than or equal to it, but before any objects greater than it. For example *

oList.insert( "Jill" );

* @param newItem The object to be inserted into the List. */ public void insert (Comparable newItem) { if (this.isEmpty()) this.addItem(newItem); else { if(((Comparable) this.getFirst()).compareTo(newItem) > 0) this.addItem(newItem); else ((OrderedList) this.getRest()).insert(newItem); } } }