数据结构之栈:
package stack;
public class StackList<T> {
public int MAXSIZE;
public T[] data;
public int top;
public StackList(int MAXSIZE, T[] data, int top) {
this.MAXSIZE = MAXSIZE;
this.data = data;
this.top = top;
}
public int push(StackList<T> st, T data){
if(st.top==st.MAXSIZE-1){
return -1;
}
st.data[top]=data;
st.top++;
return 0;
}
public T pop(StackList<T> st,T data){
if(st.top==-1){
return null;
}
data=st.data[st.top];
top--;
return data;
}
public void print(StackList<T> t){
T i=null;
while(t.top!=-1){
System.out.println(t.pop(t,i));
}
}
public static void main(String[] args){
String[] eles=new String[]{"a","b","c","d"};
StackList<String> stacklist=new StackList<String>(4,eles,0);
for(int i=0;i<stacklist.MAXSIZE-1;i++){
stacklist.push(stacklist,eles[i]);
}
stacklist.print(stacklist);
}
}