生长堆
package com.company.stucts.stack; import java.util.Arrays; // 来 自 NowJava.com - 时 代 Java public class GrowingStack { private Object[] data; private int top; public GrowingStack(int size) { if (size < 0) { throw new IllegalStateException("Stack size negative value!"); } data = new Object[size]; top = 0; /** * nowjava.com - 时代Java 提 供 **/ } public void push(Object o) { ensureCapacity(); data[top++] = o; } public Object pop() { if (top == 0) { throw new IllegalStateException("Stack is empty"); } Object o = data[--top]; data[top] = null; return o; } public boolean isEmpty() { return top != 0; } private void ensureCapacity() { if (top == data.length) { int oldSize = data.length; int newSize = oldSize * 2 + 1; System.out.println("Stack is grow! Old size: " + oldSize + ", new size: " + newSize); data = Arrays.copyOf(data, newSize); } } @Override public String toString() { return "Queue: " + "\n" + "data[]: " + Arrays.toString(data) + "\n" + "data[] length: " + data.length + "\n" + "top: " + top; }