package basictype;
import java.util.Iterator;


public class LinkedStack<T> extends Stack<T> {
  private class Link {
    T element;
    Link next;
    
    Link(T element, Link next) {
      this.element = element;
      this.next = next;
    }
  }
  
  private Link top;
  private int size;
  
  public LinkedStack() {
    this.size = 0;
    this.top = null;
  }
  
  @Override
  public void push(T item) {
    this.top = new Link(item, this.top);
    this.size++;
  }
  
  @Override
  public T top() {
    return this.top.element;
  }
  
  @Override
  public T pop() {
    T result = null;
    if (this.top != null) {
      result = this.top.element;
      this.top = this.top.next;
      this.size--;
      return result;
    } else {
      throw new RuntimeException("Error: pop on empty stack.");
    }
  }
  
  @Override
  public int size() {
    return this.size;
  }
  
  @Override
  public Iterator<T> iterator() {
    return new LinkedStackIterator();
  }

  private class LinkedStackIterator implements Iterator <T> {
    private Link currentLink = LinkedStack.this.top;
    
    @Override
    public boolean hasNext() {
      return this.currentLink != null ;
    }
    
    @Override
    public T next() {
      T result = this.currentLink.element;
      this.currentLink = this.currentLink.next;
      return result;
    }

    @Override
    public void remove() {
      // Don't do anything, removing items from a stack is for pop() only!
    }
  }
  
  
  private static final String[] defaultArgs = {
    "un", "deux", "trois", "quatre", "cinq",
    "six", "sept", "huit", "neuf", "dix",
    "onze", "douze", "treize", "quatorze", "quinze"
  };

  public static void main(String[] args) {
    String[] test = args;
    if (test.length == 0) {
      test = defaultArgs;
    }
    Stack<String> s = new LinkedStack<String>();
    for(String arg: test) {
      s.push(arg);
    }
    
    System.out.println("# Iterate");
    System.out.println(s);
    
    System.out.println("# Pop");
    while (s.size() > 0) {
      System.out.println(s.pop());
    }
  }

}
