接口
public interface Stack <E>{
int getSize();
boolean isEmpty();
void push(E e);
E pop();
E peek();
}
栈
public class ArrayStack<E> implements Stack<E> {
Array<E> array;
public ArrayStack(int Capacity){
array = new Array<>(Capacity);
}
public ArrayStack(){
this(0);
}
public int getSize(){
return array.getSize();
}
public boolean isEmpty(){
return array.isEmpty();
}
public int getCapacity(){
return array.getCapacity();
}
public void push(E e){
array.addLast(e);
}
public E pop(){
return array.removeLast(e);
}
public E peek(E){
return array.getLast()
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Stack:");
res.append('[');
for (int i = 0; i <array.getSize() ; i++) {
res.append(array.get(i));
if (i!=array.getSize()-1){
res.append(", ");
}
}
res.append("] top");
return res.toString();
}
}
时间复杂度
int getSize();
boolean isEmpty();
void push(E e);
E pop();
E peek();