数据结构(java)之循环队列

1.队列的接口类

public interface Queue<E> {

    //获取队列长度
    int getSize();
    //判断队列是否为空
    boolean isEmpty();
    //向队列中添加元素
    void enqueue(E e);
    //从队列中取出元素
    E dequeue();
    //取出队列中的首个元素
    E getFront();

}

2.循环队列接口实现类

public class LoopQueue<E> implements Queue<E> {

    private E[] data;
    private int front,tail;  //分别指向队首队尾
    private int size;  //数组中元素个数
    //留一个位置
    public LoopQueue(int capacity){
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }
    //空参构造方法
    public LoopQueue(){
        this(10);
    }
    //获取数组容量
    public int getCapacity(){
        return data.length-1;
    }
    //判断数组是否为空
    @Override
    public boolean isEmpty() {
        return front==tail;
    }
    //获取数组中的元素个数
    @Override
    public int getSize() {
        return size;
    }
    //入队操作
    @Override
    public void enqueue(E e) {
        if ((tail+1) % data.length == front)
            resize(getCapacity()*2);
        data[tail] = e;
        tail = (tail+1)%data.length;
        size++;
    }
    //出队操作
    @Override
    public E dequeue() {
        if (isEmpty())
            throw new IllegalArgumentException("无法从一个空队列中执行出队操作");
        E ret = data[front];
        data[front] = null;
        front = (front+1) % data.length;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 !=0)
            resize(getCapacity()/2);
        return ret;
    }
    //获取队首元素
    @Override
    public E getFront() {
        if (isEmpty())
            throw new IllegalArgumentException("队列为空");
        return data[front];
    }
    //扩容
    private void resize(int newCapacity){
        E[] newData  =(E[])new Object[newCapacity+1];
        for (int i=0;i<size;i++)
            newData[i] = data[(i+front) % data.length];
        data = newData;
        front = 0;
        tail = size;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size = %d , capacity = %d\n", size, getCapacity()));
        res.append("front [");
        for(int i = front ; i != tail ; i = (i+1) % data.length){
            res.append(data[i]);
            if((i+1) % data.length != tail)
                res.append(", ");
        }
        res.append("] tail");
        return res.toString();
    }
    public static void main(String[] args) {
        //新建队列类
        LoopQueue<Integer> arrayQueue = new LoopQueue<Integer>();
        //循环向队列中添加元素
        for (int i=0;i<10;i++){
            arrayQueue.enqueue(i);
            System.out.println(arrayQueue);

            //每添加三个元素从队列中取出一个元素
            if (i % 3==2){
                arrayQueue.dequeue();
                System.out.println(arrayQueue);
            }
        }
    }
}

3.执行Main方法后结果如下
Queue: size = 1 , capacity = 10
front [0] tail
Queue: size = 2 , capacity = 10
front [0, 1] tail
Queue: size = 3 , capacity = 10
front [0, 1, 2] tail
Queue: size = 2 , capacity = 5
front [1, 2] tail
Queue: size = 3 , capacity = 5
front [1, 2, 3] tail
Queue: size = 4 , capacity = 5
front [1, 2, 3, 4] tail
Queue: size = 5 , capacity = 5
front [1, 2, 3, 4, 5] tail
Queue: size = 4 , capacity = 5
front [2, 3, 4, 5] tail
Queue: size = 5 , capacity = 5
front [2, 3, 4, 5, 6] tail
Queue: size = 6 , capacity = 10
front [2, 3, 4, 5, 6, 7] tail
Queue: size = 7 , capacity = 10
front [2, 3, 4, 5, 6, 7, 8] tail
Queue: size = 6 , capacity = 10
front [3, 4, 5, 6, 7, 8] tail
Queue: size = 7 , capacity = 10
front [3, 4, 5, 6, 7, 8, 9] tail

4.循环队列的复杂度
在这里插入图片描述

发布了35 篇原创文章 · 获赞 7 · 访问量 2131

猜你喜欢

转载自blog.csdn.net/weixin_40605573/article/details/90745263