public class Stack {
private static final int l = 50;
private int top=0;
private String[] stack=new String[l];;
public void push(String string){
top++;
stack[top]= string;
}
public String pop(){
String s=stack[top];
stack[top]=null;
top--;
return s;
}
public boolean isEmpty(){
if(top==0){
return true;
}
return false;
}
public String peek(){
return stack[top];
}
public static void main(String[] args) {
Stack s=new Stack();
System.out.println(s.peek());
s.push("a");
s.push("b");
s.push("c");
System.out.println(s.pop());
System.out.println(s.peek());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.isEmpty());
}
}
我觉的挺好的,可是,在入栈前应该判断是否栈满,如果栈满了,应该追加分配空间。出栈前也应该判断下,是否已经栈空。这样会更好些。