转载请标明出处:
http://blog.csdn.net/mr_lawen/article/details/52240320
栈是一种先进后出的线性存储结构
public class Stack {
private int maxSize; //定义栈空间大小
private int array[]; //定义存放栈元素的数组
private int top; //栈顶指针
public Stack(int maxSize) {
//初始化栈
this.maxSize = maxSize;
array = new int[maxSize];
top = -1;
}
//进栈操作
public int push(int e) {
if (!isFull()) {
array[++top] = e;
return 1; //进栈成功
} else {
return -1; //栈满,不能进栈
}
}
//出站操作
public int pop() {
if (!isEmpty()) {
return array[top--]; //出栈成功
} else {
return -1; //栈空,不能出栈
}
}
//判断栈是否已经满了
public boolean isFull() {
return top == maxSize - 1;
}
//判断栈是否已经空了
public boolean isEmpty() {
return top == -1;
}
//从栈底到栈顶输出元素
public void disPlay() {
for (int i = 0; i <= top; i++) {
System.out.println(array[i]);
}
System.out.println();
}
}