Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack
- {
- private int size;
- private Object[] data;
- private int count;
- Stack(int size)
- {
- this.size = size;
- this.data = new Object[this.size];
- this.count = 0;
- }
- public void synchronized push(Object value)
- {
- if(count >= size)
- {
- throw new OverflowStackException("Overflow " + count + "/" + size);
- }
- data[count++] = value;
- }
- public Object top()
- {
- if(count <= 0)
- {
- throw new EmptyStackException("Empty " + count + "/" + size);
- }
- return data[count - 1];
- }
- public void synchronized pop()
- {
- if(count <= 0)
- {
- throw new EmptyStackException("Empty " + count + "/" + size);
- }
- data[--count] = null;
- }
- public boolean isEmpty()
- {
- return (count <= 0);
- }
- public boolean isFull()
- {
- return (count >= size);
- }
- public void synchronized clear()
- {
- for(int i = (count - 1);i >= 0;i--)
- {
- data[i] = null;
- count = 0;
- }
- }
- }
- interface StackException extends Exception
- {
- }
- class EmptyStackException implements StackException
- {
- }
- class OverflowStackException implements StackException
- {
- }
Add Comment
Please, Sign In to add comment