标签:blog java for 数据 2014 log 代码 amp size
首先是定义栈的基本结构,因为用数组实现
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;
}
标签:blog java for 数据 2014 log 代码 amp size
原文地址:http://blog.csdn.net/yexiao123098/article/details/40840973