import java.util.*;

/** 
 * A ListNode object is a singly-linked list node, which stores a value and a link to the next node.
 */
class ListNode<E> {
    public ListNode(E data, ListNode<E> next){
        this.data = data;
        this.next = next;
    }

    public E data;
    public ListNode<E> next;
}

/** 
 * A MyList object is a singly-linked list. This class implements the List interface.
 * This implementation is still under construction.
 */
class MyList<E> implements List<E> {
    public MyList(){}

    public MyList(E[] a){
        for (E e: a){
            add(e);
        }
    };

    public boolean add(E e){
        if (tail == null){
            head = tail = new ListNode<E>(e, null);
        } else {
            ListNode<E> p = new ListNode<E>(e, null);
            tail.next = p;
            tail = p;
        }
        return true;
    }

    public void add(int index, E element){
        if (index < 0) 
            throw new IndexOutOfBoundsException("add");
        if (index == 0 && head == null)
            add(element);
        ListNode<E> cur, pre;
        cur = head; pre = null;
        while (cur != null && index > 0){
            pre = cur ;
            cur = cur.next;
            index--;
        }
        if (index > 0) 
            throw new IndexOutOfBoundsException("add");
        ListNode<E> p = new ListNode<E>(element,cur);
        if (pre == null){
            head = p;
        } else {
            pre.next = p;
        }
    };

    public boolean    addAll(Collection<? extends E> c){return true;};
    public boolean    addAll(int index, Collection<? extends E> c){return true;};
    public void    clear(){};
    public boolean    contains(Object o){return true;};
    public boolean    containsAll(Collection<?> c){return true;};
    public boolean    equals(Object o){return true;};
    public E    get(int index){return null;};
    public int    hashCode(){return 0;};
    public int    indexOf(Object o){return 0;};
    public boolean    isEmpty(){
        return head == null;
    };
    public Iterator<E>    iterator(){return null;};
    public int    lastIndexOf(Object o){return 0;};
    public ListIterator<E>    listIterator(){return null;};
    public ListIterator<E>    listIterator(int index){return null;};
    public E    remove(int index){return null;};
    public boolean    remove(Object o){return true;};
    public boolean    removeAll(Collection<?> c){return true;};
    public boolean    retainAll(Collection<?> c){return true;};
    public E    set(int index, E element){
        if (index < 0)
            throw new IndexOutOfBoundsException("add");
        
        ListNode<E> cur = head;
        while (cur != null && index > 0){
            cur = cur.next;
            index--;
        }
        if (index > 0 || cur == null)
            throw new IndexOutOfBoundsException("add");
        E oldVal = cur.data;
        cur.data = element;
        return oldVal;
    }

    public int    size(){
        int count = 0;
        ListNode<E> cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    };

    public List<E>    subList(int fromIndex, int toIndex){return null;}
    public Object[]    toArray(){return new Object[0];};
    public <E> E[]    toArray(E[] a){return a;};

    public String toString(){
        StringBuffer sb = new StringBuffer();
        ListNode<E> cur = head;
        
        sb.append('[');
        while (cur != null){
            sb.append(cur.data.toString());
            if (cur != tail) sb.append(',');
            cur = cur.next;
        }
        sb.append(']');
        return sb.toString();
    }

    private ListNode<E> head, tail;

    public static void main(String[] args){
        Integer[] a = {1,2,3,4,5};
        MyList<Integer> lst = new MyList<Integer>(a);
        System.out.println(lst);
        lst.add(0,0);
        System.out.println(lst);        
    }
}
