public class Stack {
private Object[] stack;
private int top = 0; //初始化栈顶
private int size = 0; // 初始化大小
// 入栈
public void push(Object object) {
if (isFull()) {
System.out.println("栈满! 入栈失败");
}
stack[top++] = object;
}
// 出栈
public Object pop() {
Object object = stack[--top];
stack[top] = null;
return object;
}
// 计算栈当前大小
public int size() {
return top;
}
// 判断是否是空栈
public boolean isEmpey() {
return top == 0;
}
// 判断是否栈满
public boolean isFull() {
return top >= size;
}
public Stack(int size) {
this.size = size;
}
public static void main(String[] args) {
Stack stack = new Stack(3);
String[] data = new String[] { "a", "b", "c" };
for (int i = 0; i < data.length; i++) {
stack.push(data[i]);
System.out.println(data[i] + "");
}
System.out.println("***********");
while (!stack.isEmpey()) {
System.out.println(stack.pop() + "");
}
}
}
}
我是初学者,很多都不懂,麻烦哪位高手帮我看看,用简单的方法给我讲解一下哇,有很多错误,运行不起,谢谢啦,呵呵~~~