Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface Stack<T>
- {
- T top();
- Stack<T> pop();
- Stack<T> push(T newTop);
- default Stack<T> push(T newTop)
- {
- return new NonEmpty<>(newTop, this);
- }
- public class Empty<T> implements Stack<T>
- {
- public T top()
- {
- throw new EmptyStackException();
- }
- public Stack<T> pop()
- {
- throw new EmptyStackException();
- }
- }
- public class NonEmpty<T> implements Stack<T>
- {
- private T top;
- private Stack<T> tail;
- public NonEmpty(T newTop, Stack<T> previous)
- {
- top = newTop;
- tail = previous;
- }
- public T top()
- {
- return top;
- }
- public Stack<T> pop()
- {
- return tail;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment