public class StackByArrary {
private Integer[] arr;
private Integer size;
public StackByArrary(int initSize){
if(initSize<0){
throw new IllegalArgumentException("The init size is less than 0");
}
arr=new Integer[initSize];
size=0;
}
/**
* 返回栈顶的元素但是不移除
* @return
*/
public Integer peek(){
if(size==0){
return null;
}
return arr[size-1];
}
/**
* 返回栈顶的元素并移除
* @return
*/
public Integer pop(){
if(size==0){
throw new IllegalArgumentException("The stack is null");
}
return arr[--size];
}
public void push(Integer num){
if(size==arr.length){
throw new ArrayIndexOutOfBoundsException("The stack is empty");
}
arr[size++]=num;
}
}
用数组结构实现大小固定的栈
猜你喜欢
转载自blog.csdn.net/qq_42667028/article/details/86664863
今日推荐
周排行