fork download
  1. package Stack.List;
  2.  
  3. class Cell {
  4.  
  5. private Object date;
  6. private Cell next;
  7.  
  8. Object getdate() {
  9. return date;
  10. }
  11.  
  12. Cell getnext() {
  13. return next;
  14. }
  15.  
  16. Cell(Object x, Cell c) {
  17. this.date = x;
  18. this.next = c;
  19. }
  20. }
  21.  
  22. public class Stack {
  23. private Cell top;
  24. private int size = 10;
  25. private int nCell = 0;
  26.  
  27. public void push(Object o) {
  28. if (nCell >= size) throw new StackOverflowException();
  29. Cell a = new Cell(o, top);
  30. top = a;
  31. nCell++;
  32. }
  33.  
  34. public Object pop() {
  35. if (top == null) throw new StackUnderflowException();
  36. Cell o_top = top;
  37. top = top.getnext();
  38. nCell--;
  39. return o_top.getdate();
  40. }
  41.  
  42. public int size() {
  43. return nCell;
  44. }
  45.  
  46. public static class StackException extends RuntimeException {}
  47. public static class StackOverflowException extends StackException {}
  48. public static class StackUnderflowException extends StackException {}
  49. }
  50.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty