您可以创建一个非常简单的堆栈,如下所示:
public class FixedStack<T>{ private T[] stack; private int size; private int top; public FixedStack<T>(int size) { this.stack = (T[]) new Object[size]; this.top = -1; this.size = size; } public void push(T obj) { if (top >= size) throw new IndexOutOfBoundsException("Stack size = " + size); stack[++top] = obj; } public T pop() { if (top < 0) throw new IndexOutOfBoundsException(); T obj = stack[top--]; stack[top + 1] = null; return obj; } public int size() { return size; } public int elements() { return top + 1; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)