/* @(#)IntList.java   29 October 2005 */

/**
 * An IntList is a list that will store int data that may
 * be added only by append operations.
 * 
 * An IntList can supply an IntListIterator for traversal.
 */
public class IntList
{
    IntListNode head = null;
    
    IntListNode tail = null;
    
    int length = 0;
    
    
    /**
     * The default constructor that produces an empty list.
     */
    public IntList() {};
    
    
    /**
     * The constructor that produces a list with the given
     * number.
     */
    public IntList(int number) {
        append(number);
    }
    
    
    /**
     * The constructor that produces a list with the given
     * numbers appended in order.
     */
    public IntList(int[] numbers) {
        append(numbers);
    }
    
    /**
     * Appends the given number to the list.
     * 
     * This operation is done in constant time
     * independent of the current length of the list.
     */
    public void append(int number) {
        if (head == null) {
            head = tail = new IntListNode(number);
        }
        else {
            tail = tail.append(number);
        }
        
        length++;
    }
    
    
    /**
     * Appends the given numbers to the list in order.
     * 
     * This operation is done in time proportional to
     * the number of items being added
     * independent of the current length of the list.
     */
    public void append(int[] numbers) {
        if (numbers == null)
            return;
        
        int n = numbers.length;
        
        for (int i = 0; i < n; i++)
            append(numbers[i]);
    }
    
    
    /**
     * Returns the number of data items in the list.
     */
    public int getLength() {
        return length;
    }
    
    
    /**
     * Returns the IntListNode that forms the head of
     * the internal list structure.
     */
    public IntListNode getHead() {
        return head;
    }
    
    
    /**
     * Returns a new iterator to traverse the list.
     */
    public IntListIterator newIterator() {
        return new IntListIterator(this);
    }
}
