package stack;

import java.util.LinkedList;
import java.util.List;

public class StackList<E> extends AbstractStack<E> {

	private List<E> elements;

	public StackList() {
		this.elements = new LinkedList<E>();
	}

	@Override
	public boolean isEmpty() {
		return this.elements.isEmpty();
	}

	@Override
	public void push(E element) throws FullStackException {
		this.elements.add(0, element);
	}

	@Override
	public void pop() throws EmptyStackException {
		if (!this.isEmpty()) {
			this.elements.remove(0);
		} else {
			throw new EmptyStackException();
		}
	}

	@Override
	public E peek() throws EmptyStackException {
		if (!this.isEmpty()) {
			return this.elements.get(0);
		} else {
			throw new EmptyStackException();
		}
	}

	@Override
	public Stack<E> clone() {
		StackList<E> stack = new StackList<E>();
		for (E element : this.elements) {
			try {
				stack.push(element);
			} catch (FullStackException e) {
				e.printStackTrace();
			}
		}
		return stack;
	}

}
