@nalan90
2018-05-04T11:37:00.000000Z
字数 995
阅读 613
数据结构
特性
接口列表
示例代码
public interface Stack<E> {
public int getSize();
public boolean isEmpty();
public void push(E e);
public E pop();
public E peek();
}
public class ArrayStack<E> implements Stack<E> {
Array<E> array;
public ArrayStack(int capacity) {
this.array = new Array<>(capacity);
}
public ArrayStack() {
this.array = new Array<>();
}
@Override
public int getSize() {
return this.array.getSize();
}
@Override
public boolean isEmpty() {
return this.array.isEmpty();
}
public int getCapacity() {
return this.array.getCapacity();
}
@Override
public void push(E e) {
this.array.addLast(e);
}
@Override
public E pop() {
return this.array.removeLast();
}
@Override
public E peek() {
return this.array.getLast();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Stack: ");
res.append('[');
for (int i = 0; i < this.array.getSize(); i++) {
res.append(array.get(i));
if (i != this.array.getSize() - 1) {
res.append(", ");
}
}
res.append("] top");
return res.toString();
}
}