标签:
首先是定义栈的基本结构,由于用数组实现
private String[] stack; private int TOP = 0;
StackOfStrings(int capacity) { stack = new String[capacity]; }
public void push(String str) { if (TOP == stack.length) // if full, increase the stack size resize(2 * stack.length); stack[TOP++] = str; }
public String pop() { String popItem = stack[--TOP]; // if equal to 1/4, decrease the stack size if (TOP > 0 && TOP == stack.length / 4) resize(stack.length / 2); return popItem; }
public boolean isEmpty() { return TOP == 0; }
public void resize(int capacity) { String[] temp = new String[capacity]; for (int i = 0; i < TOP; i++) temp[i] = stack[i]; stack = temp; }
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4297883.html