This commit is contained in:
louiscklaw
2025-02-01 01:58:47 +08:00
parent b3da7aaef5
commit 04dbefcbaf
1259 changed files with 280657 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];
}
}