public class TestStack {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack st=new Stack(8);
st.push("new01");
st.push("new02");
st.push("new03");
System.out.println(st.size());
System.out.println(st.top());
}
}
Java實現堆棧類
public class Stack {
private int capacity = 100;
private String[] items;
private int top=0;
public Stack(){
this(100);
}
//
public Stack(int i) {
// TODO Auto-generated constructor stub
this.capacity=i;
items=new String[i];
}
//入棧
public void push(String s){
top++;
items[top]=s;
}
//出棧
public void pop(){
items[top]=null;
top--;
}
public void empty(){
top=0;
}
public String top(){
return items[top];
}
public int size(){
return top;
}
}