栈的实现,通过数组存储
public class Stack1 {
private int[] data=new int[10];
private int size=0;
private void realloc(){
int[] newData=new int[data.length*2];
for (int i = 0; i < data.length; i++) {
newData[i]=data[i];
}
data=newData;
}
public void push(int val){
if(size<data.length){
data[size]=val;
size++;
}else{
return;
}
}
public Integer pop(int val){
if(size<=0){
return null;
}
int res=data[size-1];
size--;
return res;
}
public Integer peek(int val){
if(size<=0){
return null;
}
return data[size-1];
}
public int size(){
return size;
}
public void clear(){
size=0;
}
public boolean isEmpty(){
return size==0;
}
}