package MyStack;
public class MyStack<E> {
private Object objs[];
private int size;
public MyStack(){
this(10);
}
public MyStack(int size){
objs = new Object[size];
this.size = 0;
}
public E push(Object o){
ensureCapacity();
objs[size++] = o;
return (E)o;
}
public E pop(){
Object o = objs[size-1];
if(empty()){
return null;
}else{
size--;
}
return (E)o;
}
public E peek(){
Object o = objs[size-1];
if(empty()){
return null;
}
return (E)o;
}
public boolean empty(){
return size == 0?true:false;
}
public int size(){
return size;
}
public void ensureCapacity(){
if(size == objs.length){
Object temp[] = objs;
objs = new Object[temp.length + 10];
for(int i=0;i<size;i++){
objs[i] = temp[i];
}
}
}
public int search(Object o){
int index = -1;
if(size == 0){
index = -1;
}else{
for(int i=0;i<size;i++){
if(o.equals(objs[i])){
index = i+1;
}
}
}
return index;
}
}
数据结构之栈的数组实现
最新推荐文章于 2023-05-15 12:30:00 发布