Stack(栈)
public interface Stack<T> {
boolean isEmpty();
void push(T var1);
T peek();
T pop();
}
SeqStack(顺序栈)
public final class SeqStack<T> implements Stack<T> {
private final SeqList<T> list;
public SeqStack(int length) {
this.list = new SeqList<>(length);
}
public SeqStack() {
this(64);
}
public boolean isEmpty() {
return this.list.isEmpty();
}
public void push(T x) {//入栈
this.list.insert(x);
}
public T peek() {//返回栈顶元素
return this.list.get(this.list.length() - 1);
}
public T pop() {//返回栈顶元素
return this.list.remove(this.list.length() - 1);
}
public String toString() {
return this.getClass().getName() + " " + this.list.toString();
}
}
LinkedStack(链式栈)
public final class LinkedStack<T> implements Stack<T> {
private final SinglyList<T> list = new SinglyList<>();
public LinkedStack() {
}
public boolean isEmpty() {
return this.list.isEmpty();
}
public void push(T x) {
this.list.insert(0, x);
}
public T peek() {
return this.list.get(0);
}
public T pop() {
return this.list.remove(0);
}
public String toString() {
return this.getClass().getName() + " " + this.list.toString();
}
}
写在最后:
仅为学习阶段记录写过的代码,方便自己查找,初出茅庐,有不完善的地方烦请指正,如有继承类可查阅数据结构其他博文。