实现一个简单的StackOfIntegers程序。
package temp;
class stack{
private int element[];//用数组来定义栈空间
private int size = 0;//定义栈中元素的个数
public static final int defaultCapacity = 10;
//构造函数
public stack(){
this(defaultCapacity);
}
public stack(int capacity){
element = new int[capacity];
}
//入栈
public void push(int elem){
if(size >= element.length){
int temp[] = new int[element.length * 2];
System.arraycopy(element,0,temp,0,element.length);
element = temp;
element[size++] = elem;
}
else{
element[size++] = elem;
}
}
//出栈
public void pop(){
System.out.print(element[--size] + " ");
}
//返回栈顶元素
public int peek(){
return element[size-1];
}
//判空方法
public boolean isprime(){
if(size <= 0)return false;
else return true;
}
}
public class temp {
public static void main(String[] args){
stack instance = new stack();
for(int i = 0 ; i < 9 ; i++){
instance.push(i);
System.out.print(i + " ");
}
System.out.println();
for(int i = 0 ; i < 9 ; i++){
instance.pop();
}
}
}