All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.core.stack.a.LArrayStack Maven / Gradle / Ivy

There is a newer version: 10.3.1
Show newest version
package shz.core.stack.a;

import java.util.Arrays;

/**
 * 元素类型为E基于动态数组的栈
 * 

* 24+[8+E(类型字节)]*n(n为元素个数)=es *

* B=48+8*n+(E*n+对齐填充) */ @SuppressWarnings("unchecked") public class LArrayStack extends ArrayStack { protected E[] es; protected LArrayStack(int capacity) { super(capacity); es = (E[]) new Object[capacity]; } public static LArrayStack of(int capacity) { return new LArrayStack<>(capacity); } public static LArrayStack of() { return of(DEFAULT_CAPACITY); } @Override protected final E get(int i) { return es[i]; } @Override protected final void resize(int capacity) { this.capacity = capacity; es = Arrays.copyOf(es, capacity); } @Override protected final void setNull(int i) { es[i] = null; } public final void push(E e) { beforePush(); es[size++] = e; } public final E pop() { E e = es[--size]; afterPop(); return e; } public final E peek() { return es[size - 1]; } public final void reverse() { if (size == 0) return; //栈底移至栈顶 bottomToTop(); E top = es[size - 1]; pop(); reverse(); push(top); } private void bottomToTop() { if (size == 0) return; E top1 = es[size - 1]; pop(); if (size == 0) push(top1); else { bottomToTop(); E top2 = es[size - 1]; pop(); //交换栈顶 与 子栈顶 push(top1); push(top2); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy