import java.util.Optional;
public class Stack<T> {
Node<T> top;
public void push(T value) {
top = new Node<T>(value, top);
}
public Optional<T> pop() {
if (top == null)
return Optional.empty();
var result = top.value;
top = top.next;
return Optional.of(result);
}
}
import java.util.Optional;
public class ArrayStack<T> {
Object[] stack;
Integer top;
public ArrayStack(Integer size) {
stack = new Object[size];
top = 0;
}
public Boolean isFull() {
return top == stack.length - 1;
}
public void push(T value) throws IndexOutOfBoundsException {
if (isFull())
throw new IndexOutOfBoundsException();
stack[top] = value;
top++;
}
public Optional<Object> pop() {
if (top == 0)
return Optional.empty();
var result = stack[top];
top--;
return Optional.of(result);
}