循环队列

定义一个接口

public interface Queue {
    //计算队列的大小
    int getSize();
    //判空
    boolean isEmpty();
    boolean contains(E e);
    //入队
    void enqueue(E e);
    //出队
    E dequeue();
    //获得队首元素
    E getFront();
}

定义一个类,继承该接口

/**
 * 循环队列
 * @author hcc
 *
 * @param 
 *  规定数组的开始为队首 数组的末尾为队尾 (数据从队尾进入,队首出去)
 */
public class HLoopQueue implements Queue {
    private static final int DEFAULT_CAPACITY = 10;
    private E[] data;
    private int front;
    private int tail;
    private int size;
    public HLoopQueue() {
        this(DEFAULT_CAPACITY);
    }
    
    @SuppressWarnings("unchecked")
    public HLoopQueue(int capacity) {
        data = (E[]) new Object[capacity];
        front = 0;
        tail = 0;
        size = 0;
    }
    
    /**
     * @return 队列的容量
     */
    public int getCapacity() {
        return data.length;
    }
    
    @Override
    public int getSize() {
        // TODO Auto-generated method stub
        return size;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        if(front == tail) {
            return true;
        }
        return false;
    }
    /**
     * 判断队列是否已满
     * @return true表示已满 false表示未满
     */
    public boolean isFull() {
//      if(front == ((tail+1)%getCapacity())) {
//          return true;
//      }
//      return false;
        return front == ((tail+1)%getCapacity());
    }
    
    @Override
    public boolean contains(E e) {
        // TODO Auto-generated method stub
        if(e != null) {
            for(int i=0;i

你可能感兴趣的:(循环队列)