import java.util.ArrayList;
import java.util.Iterator;

public class MyList<E> implements Iterable<E> {

  private class Node<E> {
    public E e;
    Node<E> next;
    Node<E> prev;

    public Node(E e, Node<E> n, Node<E> p) {
      this.e = e;
      next = n;
      prev = p;
    }
  }

  private Node<E> first = null;
  private Node<E> last = null;

  public MyList() {
    first = new Node<E>(null, null, null);
    last = new Node<E>(null, null, null);
    first.next = last;
    last.prev = first;
  }

  private Node<E> getNode(int i) {
    int counter = 0;
    Node<E> aux = first.next;
    for (; counter < i; aux = aux.next) {
      counter++;
    }
    return aux;
  }

  public void add(E e) {
    Node<E> aux = last;
    aux.e = e;
    last = new Node<E>(null, null, aux);
    aux.next = last;
  }

  public void set(int i, E e) {
    Node<E> aux = getNode(i);
    aux.e = e;
  }

  public E get(int i) {
    return getNode(i).e;
  }

  private class MyIterator implements Iterator<E> {
    Node<E> current;
    Node<E> first;
    Node<E> end;

    public MyIterator(Node<E> first, Node<E> last) {
      this.first = first;
      current = first.next;
      end = last;
    }

    public boolean hasNext() {
      return current != end;
    }

    public E next() {
      E e = current.e;
      current = current.next;
      return e;
    }

    public void remove() {
      current.prev = current.prev.prev;
      current.prev.next = current;
    }
  }

  public Iterator<E> iterator() {
    return new MyIterator(first, last);
  }

}
