// Implementation for an Ordered List ADT with integer data
// File:    OrderedList.java
// Based on: C++ code list-class.h
// Author:  Henry M. Walker
// Date:    March 13, 2002

// Ordered List ADT header, based on List ADT
class OrderedList extends SimpleList{//follows SimpleList, except for insert
    public void insert (int item) {
        // pre:  the list has been initialized
        // post:  the item is placed in the list to maintain ordering by <
        if ((first == null) || (item < first.getData()))
            first = new ListNode (item, first);
        else
            recInsert(item, first);
    }

    protected void recInsert (int item, ListNode front) {
        // a recursive kernel to insert -- looking ahead one node to find location for insertion
        if ((front.getNext() == null) || (item < front.getNext().getData()))
            { ListNode temp = new ListNode(item);
            temp.setNext(front.getNext());
            front.setNext(temp);
            } else { 
                recInsert (item, front.getNext());
            }
    }

    public static void main (String argv[]) {
        // construct a test list (2 3 5 6 7)
        OrderedList a = new OrderedList();

        System.out.println ("Insert 3");
        a.insert(3);
        System.out.println ("Insert 5");
        a.insert(5);
        System.out.println ("Insert 7");
        a.insert(7);
        System.out.println ("Insert 6");
        a.insert(6);
        System.out.println ("Insert 2");
        a.insert(2);
        System.out.println ("New list:  " + a);
        System.out.println ("      length:   " + a.length());
        System.out.println ("      null?:    " + a.isNull());
        a.print();
        a.printReverse();
        System.out.println ("      check if 4 is on list:      " + a.isin(4));
        System.out.println ("      check if 5 is on list:      " + a.isin(5));
    }
} // OrderedList

