import java.util.*;
public class StringLinkedList implements Iterable<String> {

    // fields
    private StringNode head;
    
    // constructor
    public StringLinkedList() {
        this.head = null;
    }
    
    // methods
    public void add( String new_data ) {
        this.head = new StringNode( new_data, this.head );
    }
    
    public int size() {
        if (this.head == null) {
            return 0;
        }
        StringNode ptr = this.head;
        int counter = 1;
        while (ptr.next != null) {
            counter++;
            ptr = ptr.next;
        }
        return counter;
    }
    
    public String get( int idx ) {
        if (idx < 0 || idx >= this.size()) {
            return null;
        }
        StringNode curr_ptr = this.head;
        for (int curr_idx = 0; curr_idx < idx; curr_idx++) {
            curr_ptr = curr_ptr.next;
        }
        return curr_ptr.data;
    }
    
    public Iterator<String> iterator() {
        return new StringLinkedListIterator( this.head );
    }
    
    // inner class
    private class StringLinkedListIterator implements Iterator<String> {
        private StringNode next_node;
    
        public StringLinkedListIterator(StringNode node) {
            this.next_node = node;
        }
        
        public boolean hasNext() {
            return next_node != null;
        }
        
        public String next() {
            if (next_node == null) {
                return null;
            }
            String ret = next_node.data;
            next_node = next_node.next;
            return ret;
        }
        
        public void remove() { ; }
    } // end StringLinkedListIterator
    

    // inner class
    private class StringNode {
        private StringNode next;
        private String data;
        
        public StringNode( String d, StringNode n ) {
            this.data = d;
            this.next = n;
        }        
    } // end StringNode class
    
    // test code for the linked list class
    public static void main(String[] args) {
        StringLinkedList list = new StringLinkedList();
        System.out.println( list.size() );
        list.add( "Stephanie" );
        System.out.println( list.size() );
        list.add( "test" );
        System.out.println( list.size() );
        list.add( "icecream" );
        System.out.println( list.size() );
        
        for (int i =0; i < list.size(); i++ ) {
            System.out.println( list.get(i) );
        }
        
        for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
            String item = iterator.next();
            System.out.println( item );
        }  
        
        for (String item: list) {
            System.out.println( item );
        }          
    }
    
} // end StringLinkedList class    