一道java题,求大神帮忙做一下

2024-11-25 10:35:54
推荐回答(1个)
回答1:

定义的接口
public interface StackInterface {
void push(Object value);
Object pop();
boolean isEmpty();
}

实现类:
public class StackImplement implements StackInterface{
private Vector vector;
public StackImplement(){
this.vector = new Vector();
}
@Override
public boolean isEmpty() {
return vector.isEmpty();
}
@Override
public Object pop() {
Object obj;
int len = vector.size();

if(len==0){
throw new EmptyStackException();
}
obj = vector.elementAt(len-1);
vector.removeElementAt(len - 1);
return obj;
}
@Override
public void push(Object value) {
vector.addElement(value);
}
}

Stack的底层就是类似实现的!望参考!