队列的核心为先进先出,即先入队的元素先出队,在之前手写的ArrayList中添加了删除方法实现了队列
[mw_shl_code=java,true]
/**
* 在之前自定义的动态数组基础上完成队列,动态数组中要添加删除方法
*
* @author 大刘
*/
public class Queue <E>{
private ArraysList<E> arraysList;
/**
* 入队方法
* @param e 入队元素
*/
public void enqueue(E e){
arraysList.add(e);
}
/**
* 出队方法 取出对头元素
* @return
*/
public E dequeue(){
return arraysList.removeFirst();
}
public boolean isEmpty(){
return arraysList.isEmpty();
}
/**
* 查看队头元素
* @return
*/
public E getHead(){
return arraysList.get(0);
}
public int size(){
return arraysList.size;
}
}
[/mw_shl_code]
还有之前补充的删除方法
[mw_shl_code=java,true] private E remove(int index){
if(size==0){
throw new IndexOutOfBoundsException("数组空");
}
if(index<0||index>array.length){
throw new IllegalArgumentException("索引违法");
}
E e=array[index];
for(int i=index;i<size;i++){
array=array[i+1];
}
size--;
return e;
}
public E removeFirst(){
return remove(0);
}
public E removeLast(){
return remove(size-1);
}
[/mw_shl_code]
转载自CSDN
|
|