package 二叉查找输;
import java.util.Arrays;
public class stack<E> {
private Object[] stack;
private int size;
public stack(){
stack=new Object[10];
}
public boolean isEmpty(){
return size==0;
}
public E peek(){
if(isEmpty()){
return null;
}
return (E)stack[size-1];
}
public E pop(){
E e=peek();
stack[size-1]=null;
size--;
return e;
}
public void ensureCapacity(int size){
if(size>stack.length){
int newLen=10;
Arrays.copyOf(stack, newLen);
}
}
public E push(E item){
ensureCapacity(size+1);
stack[size]=item;
size++;
return item;
}
public static void main(String[] args){
stack<Integer> s=new stack<Integer>();
s.push(1);
s.push(3);
s.push(80);
s.push(23);
System.out.println(s.size);
System.out.println(s.pop());
System.out.println(s.pop());
}
}
JAVA基础训练——用数组实现栈的功能
最新推荐文章于 2022-05-01 12:54:29 发布