Guest User

Untitled

a guest
Jul 16th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. class Stack
  2. {
  3.  
  4. private int size;
  5. private Object[] data;
  6. private int count;
  7.  
  8. Stack(int size)
  9. {
  10. this.size = size;
  11. this.data = new Object[this.size];
  12. this.count = 0;
  13. }
  14.  
  15. public void synchronized push(Object value)
  16. {
  17. if(count >= size)
  18. {
  19. throw new OverflowStackException("Overflow " + count + "/" + size);
  20. }
  21. data[count++] = value;
  22. }
  23.  
  24. public Object top()
  25. {
  26. if(count <= 0)
  27. {
  28. throw new EmptyStackException("Empty " + count + "/" + size);
  29. }
  30. return data[count - 1];
  31. }
  32.  
  33. public void synchronized pop()
  34. {
  35. if(count <= 0)
  36. {
  37. throw new EmptyStackException("Empty " + count + "/" + size);
  38. }
  39. data[--count] = null;
  40. }
  41.  
  42. public boolean isEmpty()
  43. {
  44. return (count <= 0);
  45. }
  46.  
  47. public boolean isFull()
  48. {
  49. return (count >= size);
  50. }
  51.  
  52. public void synchronized clear()
  53. {
  54. for(int i = (count - 1);i >= 0;i--)
  55. {
  56. data[i] = null;
  57. count = 0;
  58. }
  59. }
  60. }
  61.  
  62. interface StackException extends Exception
  63. {
  64.  
  65. }
  66.  
  67. class EmptyStackException implements StackException
  68. {
  69.  
  70. }
  71.  
  72. class OverflowStackException implements StackException
  73. {
  74.  
  75. }
Add Comment
Please, Sign In to add comment