基本功能
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-G3LRZZmK-1610469089793)(D3C0144A7FD44573B9FF652E616CF7C6)]
面向对象迭代
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hNhxU9NF-1610469089796)(E0BA029B2D7F4D93A25CFCAED164BEFB)]
【Stack接口】
public class Main {
public static void main(String[] args) {
ArrayStack<Integer> stack = new ArrayStack<>();
for (int i = 0; i < 5; i++) {
stack.push(i);
System.out.println(stack);
}
stack.pop();
System.out.println(stack);
}
}
【实现】
- 结成了Array类
public class ArrayStack<E> implements Stack<E> {
Array<E> array;
public ArrayStack(int capacity){
array = new Array<>(capacity);
}
public ArrayStack(){
array = new Array<>();
}
@Override
public int getSize() {
return array.getSize();
}
public int getCapacity(){
return array.getCapacity();
}
@Override
public boolean isEmpty() {
return array.isEmpty();
}
@Override
public void push(E e) {
array.addLast(e);
}
@Override
public E pop() {
return array.removeLast();
}
@Override
public E peek() {
return array.getLast();
}
@Override
public String toString(){
StringBuilder sbr = new StringBuilder();
sbr.append("Stack:");
sbr.append('[');
for (int i = 0; i < array.getSize(); i++) {
sbr.append(array.get(i));
if(i != array.getSize() - 1){
sbr.append(",");
}
}
sbr.append("] top");
return sbr.toString();
}
}
时间复杂度分析
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TmywZe0r-1610469089799)(230914B90E95429EA559F4AD9B3C8427)]