Simple implementation of a generic stack. Uses a static inner Node class.
Methods supported:
public class Stack<T> {
// inner Node class
private static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
}
// inner Node class end
private Node<T> top;
public void push(T data) {
Node<T> node = new Node<>(data);
node.next = top;
top = node;
}
public T pop() {
if (top == null) throw new EmptyStackException();
T val = top.data;
top = top.next;
return val;
}
public T peek() {
if (top == null) throw new EmptyStackException();
return top.data;
}
public boolean isEmpty() {
return top == null;
}
}