顺序栈的实现:
package LineStructure;
//参考 https://blog.csdn.net/icarus_wang/article/details/79561863
public class Stack<T> extends Object{
int MaxLength = 10;
T[] Array;
int top = -1;//栈顶 初始为-1
//默认构造
public Stack(){
this.MaxLength = MaxLength;
this.Array = (T[]) new Object[MaxLength];//初始化数组
this.top = -1;
}
//长度构造
public Stack(int maxLength){
this.MaxLength = maxLength;
this.Array = (T[]) new Object[MaxLength];
this.top = -1;
}
//push value
public void push(T value) {
this.Array[++top] = value;
}
//pop
public T pop() {
T result = Array[top];
Array[top] = null;
top--;
return result;
}
//length
public int getLength() {
return this.top+1;
}
//getTop
public T getTop() {
return Array[top];
}
public Boolean isFull() {
if (top == MaxLength) {
return true;
}else {
return false;
}
}
}