This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
public class ArrayStack implements Stack {
public static final int CAPACITY =1000;
private int capacity;
private Object[] array;
private int top =-1;
public ArrayStack() {
this(CAPACITY);
}
public ArrayStack(int cap){
capacity =cap;
array = new Object[capacity];
}
@Override
public int size() {
return top+1;
}
@Override
public boolean isEmpty() {
return (top<0);
}
@Override
public void push(Object item) throws StackFullException {
if(size()==capacity)
throw new StackFullException();
array[++top]=item;
}
@Override
public Object pop() throws StackEmptyException {
if(isEmpty())
throw new StackEmptyException();
Object item =array[top];
array[top--]=null;
return item;
}
@Override
public Object top() throws StackEmptyException {
if(isEmpty())
throw new StackEmptyException ();
return array[top];
}
}