Implement Stack in Java.
public class MyStack<E> {
private static class Node<E> {
E value;
Node<E> next;
public Node(E v, Node<E> n){
value = v; next = n;
}
}
private Node<E> head;
private int size = 0;
public void push(E e) {
head = new Node<E>(e, head);
size++;
}
public E pop() throws Exception {
if(size == 0) throw new Exception("Empty Stack!");
E e = head.value;
head = head.next;
size--;
return e;
}
public E peek() throws Exception {
if(size == 0) throw new Exception("Empty Stack!");
return head.value;
}
public int size() {
return size;
}
}