fork download
  1. package Stack.Array;
  2.  
  3. public class Stack{
  4. private Object[] stack;
  5. private int size = 10;
  6. private int stack_pointer = -1;
  7.  
  8. public Stack(){
  9. stack = new Object[size];
  10. }
  11.  
  12. public void push(Object o) {
  13. if (stack_pointer >= size - 1) throw new StackOverflowException();
  14. stack_pointer++;
  15. stack[stack_pointer] = o;
  16. }
  17.  
  18. public Object pop() {
  19. if (stack_pointer < 0) throw new StackUnderflowException();
  20. Object o = stack[stack_pointer];
  21. stack_pointer--;
  22. return o;
  23. }
  24.  
  25. public int size() {
  26. return stack_pointer + 1;
  27. }
  28.  
  29. public static class StackException extends RuntimeException {}
  30. public static class StackOverflowException extends StackException {}
  31. public static class StackUnderflowException extends StackException {}
  32. }
  33.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty