栈
import java.util.Arrays;
/**
*
* <栈>
* <后进先出>
* @author
* @version [版本号, 2011-9-19]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class SequenceStack<T>{
private int DEFAULT_INIT_SIZE = 10;
//保存数组的长度
private int capacity;
//定义当底层容量不够时,程序每次增加的数组长度
private int capacityIncrement = 0;
//定义一个数组,用于保存顺序栈的元素
private Object[] elementData;
//保存顺序栈中元素的当前个数
private int size = 0;
/**
* <以默认数组长度创建空顺序栈>
*/
public SequenceStack(){
capacity = DEFAULT_INIT_SIZE;
elementData = new Object[capacity];
}
/**
* <以一个初始化元素来创建顺序栈>
* @param element 指定顺序栈中第一个元素
*/
public SequenceStack(T element){
this();
elementData[0] = element;
size++;
}
/**
* <以指定长度的数组来创建顺序栈>
* @param element 指定顺序栈中第一个元素
* @param initSize 指定顺序栈底层数组的长度
*/
public SequenceStack(T element, int initSize){
this.capacity = initSize;
elementData = new Object[capacity];
elementData[0] = element;
size++;
}
/**
* <以指定长度的数组来创建顺序栈>
* @param element 指定顺序栈中第一个元素
* @param initSize 指定顺序栈底层数组的长度
* @param capacityIncrement 定义当底层容量不够时,程序每次增加的数组长度
*/
public SequenceStack(T element, int initSize, int capacityIncrement){
this.capacity = initSize;
this.capacityIncrement = capacityIncrement;
elementData = new Object[capacity];
elementData[0] = element;
size++;
}
/**
* <获取顺序栈的大小>
* @return [参数说明]
*
* @return int [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
public int length(){
return size;
}
/**
* <入栈>
* @param element [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
public void push(T element){
//栈长度+1
ensureCapacity(size+1);
//将要添加的元素放在栈顶
elementData[size++] = element;
}
/**
* <出栈>
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
@SuppressWarnings("unchecked")
public synchronized T pop(){
if (isEmpty()){
throw new IndexOutOfBoundsException("栈为空");
}
//获取栈顶元素
T topValue = (T)elementData[size-1];
//释放栈顶元素
elementData[--size] = null;
return topValue;
}
/**
* <获取栈顶元素>
* @return [参数说明]
*
* @return T [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
@SuppressWarnings("unchecked")
public synchronized T peek(){
if (isEmpty()){
return null;
}
return (T)elementData[size-1];
}
/**
* <判断栈是否为空>
* @return [参数说明]
*
* @return boolean [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
public boolean isEmpty(){
return size == 0;
}
/**
* <清空栈>
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
public void clear(){
//将数组中的所有元素赋为null
Arrays.fill(elementData, null);
size = 0;
}
/**
* <扩容>
* @param minCapacity [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
public void ensureCapacity(int minCapacity){
//如果数组的原有长度小于目前所需的长度
if (minCapacity > capacity){
if (capacityIncrement > 0){
while (capacity < minCapacity){
/*
* 不断地将capacity长度加capacityIncrement,
* 直到capacity大于minCapacity
*/
capacity += capacityIncrement;
}
}else{
while (capacity < minCapacity){
/*
* 不断地将capacity*2,
* 直到capacity大于minCapacity
*/
capacity <<= 1;
}
}
elementData = Arrays.copyOf(elementData, capacity);
}
}
public String toString(){
if (isEmpty()){
return "[]";
}
StringBuilder sb = new StringBuilder("[");
for (int i=size-1; i>-1; i--){
sb.append(elementData[i].toString()+", ");
}
int len = sb.length();
return sb.delete(len-2, len).append("]").toString();
}
}
测试:
public class Test
{
/** <一句话功能简述>
* <功能详细描述>
* @param args [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
* @author
* @date 2011-9-19
*/
public static void main(String[] args)
{
SequenceStack<String> stack = new SequenceStack<String>();
System.out.println("is empty:"+stack.isEmpty());
stack.push("aaaa");
stack.push("bbbb");
stack.push("cccc");
stack.push("dddd");
System.out.println("is empty:"+stack.isEmpty());
System.out.println(stack);
//访问栈顶元素
System.out.println("栈顶元素:"+stack.peek());
//弹出一个元素
System.out.println("第一次弹出栈顶元素:"+stack.pop());
//再次弹出一个元素
System.out.println("第二次弹出栈顶元素:"+stack.pop());
System.out.println("两次pop之后的栈:"+stack);
}
}