public class Stack
{
//栈存储数组
private int[] elements;
//栈的size
private int size;
//
public static final int DEFAULT_CAPACITY = 16;
//构建一个默认容量为16的空栈
public Stack()
{
this(DEFAULT_CAPACITY);
}
//构建一个指定容量的空栈
public Stack(int capacity)
{
this.elements = new int[capacity];
}
/** Push a new integer to the top of the stack */
public void push(int value)
{
if(size >= this.elements.length)
{
int[] temp = new int[elements.length * 2];
System.arraycopy(elements, 0, temp, 0, this.elements.length);
this.elements = temp;
}
elements[this.size++] = value;
}
/** Return and remove the top element fromm the stack **/
public int pop()
{
return this.elements[--this.size];
}
/** Return the top element from the stack **/
public int peek()
{
return this.elements[this.size-1];
}
/** Test whether the stack is empty **/
public boolean empty()
{
return size == 0;
}
/** Return the number of elements in the stack **/
public int getSize()
{
return this.size;
}
}