3.1.2 (1)顺序栈 页码P42
package hicc.ds.c02_linear2;
import java.util.Arrays;
public class SeqStack {
private int maxSize = 10; //最大存储长度
private Object[] data = new Object[maxSize]; //顺序栈的存储空间
private int top; //栈顶元素的位置
public SeqStack() { //初始化空栈
top = -1;
}
public boolean emptySeqStack() { //栈空判别算法
if (top == -1) {
return true;
} else {
return false;
}
}
public int pushSeqStack(Object x) { //入栈算法
if (top == maxSize-1) {
return 0;
} else {
top++;
data[top] = x;
return 1;
}
}
public Object popSeqStack() { //出栈算法
if(emptySeqStack()) {
return null;
} else {
Object x = data[top];
top--;
return x;
}
}
public void showData() { //展示栈内数据
System.out.println(Arrays.toString(data));
System.out.println(top);
}
public static void main(String[] args) { //测试
SeqStack seqStack = new SeqStack();
seqStack.pushSeqStack('A');
seqStack.pushSeqStack('B');
seqStack.pushSeqStack('C');
seqStack.showData();
Object x = seqStack.popSeqStack();
System.out.println(x);
seqStack.showData();
}
}