定义一个接口
public interface Queue<E> {
//计算队列的大小
int getSize();
//判空
boolean isEmpty();
boolean contains(E e);
//入队
void enqueue(E e);
//出队
E dequeue();
//获得队首元素
E getFront();
}
定义一个队列的类,继承该接口
import com.hcc.Interface.Queue;
/**
* 链式队列(基于动态数组)
* @author hcc
*
* @param <E> 泛型
* 规定数组的开始为队首 数组的末尾为队尾 (数据从队尾进入,队首出去)
*/
public class HArrayQueue<E> implements Queue<E> {
private HGenericityArrayList<E> data;
public HArrayQueue() {
data = new HGenericityArrayList<E>();
}
public HArrayQueue(int capacity) {
data = new HGenericityArrayList<E>(capacity);
}
/**
* 获取队列的容量
* @return
*/
public int getCapacity() {
return data.getCapacity();
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return data.getSize();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return data.isEmpty();
}
@Override
public boolean contains(E e) {
// TODO Auto-generated method stub
return data.contains(e);
}
@Override
public void enqueue(E e) {
// TODO Auto-generated method stub
data.add(e);
}
@Override
public E dequeue() {
// TODO Auto-generated method stub
return data.remove(0);
}
@Override
public E getFront() {
// TODO Auto-generated method stub
return data.get(0);
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append("HArrayQueue:");
str.append("Front ");
str.append("[");
for(int i = 0;i < data.getSize();i++) {
if(i == (data.getSize()-1)) {
str.append(data.get(i));
str.append("]");
break;
}
str.append(data.get(i));
str.append(",");
}
str.append(" Tail");
return str.toString();
}
}
网友评论