UNB/ CS/ David Bremner/ teaching/ old/ cs1083/ java/ SortedList.java
public class SortedList<T extends Comparable<T>>{
    private ComparableNode<T> first;
    public SortedList(){
        first = null;
    }

    private ComparableNode<T> getFirst(){
        return first;
    }


    public boolean isEmpty(){
        return (first==null);
    }

    public void insert(T key){
        ComparableNode<T> newNode=new ComparableNode<T>(key);
        insert(newNode);
    }
    public void insert(ComparableNode<T> newNode){

        if (first==null || newNode.compareTo(first) < 0){
            newNode.setNext(first);
            first=newNode;
        }  else {
            ComparableNode<T> current = first, previous=null;
            while ( current != null && newNode.compareTo(current) > 0 ) {
                previous = current;
                current = current.getNext();
            }

            previous.setNext(newNode);
            newNode.setNext(current);
        }
    }
    public void print(){
        for (ComparableNode<T> cursor=getFirst(); cursor != null ;
             cursor=cursor.getNext()){
            System.out.println(cursor.getData());
        }
    }


    public static void main(String[] args){

        String[] names={"bob", "rudolph", "dasher", "prancer", "dancer", "mary","joe","fred"};

        SortedList<String> a=new SortedList<>();

        for (int i=0; i < names.length; i++){
            a.insert(names[i]);
        }

        a.print();
    }

} // class SortedList





//@keywords linked list, week 10, sort, insert