二元式栈。
TwoItemStack.java:
package per.eyuan.util;
public class TwoItemStack {
private TwoItem tis[]=new TwoItem[20];
private int top;//栈顶
public TwoItemStack() {
super();
init();
}
public void init(){
for(int i=0;i<tis.length;i++){
tis[i]=new TwoItem();
}
top=-1;
}
//获取栈中元素的个数
public int getLength(){
return top+1;
}
public void push(TwoItem ti){
top++;
tis[top]=ti;
}
public TwoItem pop(){
if(top==-1){
return null;
}else{
TwoItem tipop=tis[top];
top--;
return tipop;
}
}
public TwoItem getTop(){
if(top==-1)
return null;
else
return tis[top];
}
public TwoItem getNextTop(){
return tis[top+1];
}
public TwoItem[] getAll(){
//返回栈中所有元素
if(top==-1){
System.out.println("null,the stack is empty");
return null;
}else{
int i=0;
TwoItem ati[]=new TwoItem[top+1];
for(int ii=0;ii<ati.length;ii++){
ati[ii]=new TwoItem();
}
while(i<=top){
ati[i]=tis[i];
i++;
}
return ati;
}
}
public TwoItem[] getHole(){
if(top==-1){
System.out.println("null,the stack is empty");
return null;
}else{
return tis;
}
}
}