使用java语言实现一个队列(两种实现比较)(数据结构)

一.什么是队列,换句话说,队列主要特征是什么?

四个字:先进先出

六个字:屁股进,脑袋出

脑补个场景:日常排队买饭,新来的排在后面,前面打完饭的走人,这就是队列;

OK,思考一个问题,我为什么写了两种实现,它们的区别是什么,哪个性能更好一些?

我觉得学习一定要带着问题来学习;

 

二.队列的两种实现

1.数组队列

数组队列比较简单,基于之前写的动态数组所实现的,基本方法都是根据队列特性从而选择性的调用动态数组的方法来实现的。

 

public class ArrayQueue<E> implements Queue<E> {
    private Array<E> array;

    public ArrayQueue (int catacity) {
        array = new Array<E>(catacity);
    }

    public ArrayQueue () {
        array = new Array<E>();
    }
    //查看队列容量
    public int catacity () {
        return array.getCapacity();
    }
    //获取队列中元素个数
    @Override
    public int getSize() {
        return array.getSize();
    }
    //判断队列是否为空
    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }
    //入队 时间复杂度:O(1)
    @Override
    public void enqueue(E e) {
        array.addLast(e);
    }
    //出队 时间复杂度:O(n)
    @Override
    public E dequeue() {
        return array.removeFirst();
    }
    //查看队首元素 时间复杂度: O(1)
    @Override 
    public E getFront() {
        return array.getFirst();
    }
    @Override
    public String toString () {
        StringBuilder res = new StringBuilder();
        res.append("Queue");
        res.append("front [");
        for (int i = 0; i < getSize(); i++) {
            res.append(array.getByIndex(i));
            if (i != array.getSize() - 1) {
                res.append(",");
            }
        }
        res.append("] tail");
        return res.toString();
    }

    public static void main (String[] args) {
        ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
        for(int i = 0; i < 10; i++) {
            arrayQueue.enqueue(i);
            System.out.println(arrayQueue);
            if (i % 3 == 2) {
                System.out.println(arrayQueue);
            }
        }
    }
}

 

 

2.循环队列

什么是循环队列?用图来说明

(1)队首在0位置,队尾在4位置,队列里面有4个元素

 

 

 (2)此时,我进行两次出队操作,队首位置为2,队首前面空了两个空间。

 

 

 

 

 

 

 

 (3)我再进行三次入队操作,其中一个元素插到了数组的前面。

 

 

 

循环队列:就是当数组末端没有位置时,新的入队元素则插到数组前面空余的空间中,避免了空间的浪费,可以把整个数组想象成一个环状。

 

代码实现:

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

    private E[] data;
    //队首
    private int front;
    //队尾
    private int tail;
    //队列中元素个数
    private int size;

    public LoopQueue (int catacity) {
        //实际开辟的容量比用户定义的容量大一位
        data = (E[])new Object[catacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueue () {
        this(10);
    }
    //获取队列容量
    public int getCapacity () {
        return data.length - 1;
    }
    //获取队列元素个数
    @Override
    public int getSize() {
        return size;
    }

当front == tail 时,定义队列为空

//队列是否为空
    @Override
    public boolean isEmpty() {
        return tail == front;
    }
    //入队
    @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("Cannot dequeue from an empty queue.");
        }
        E set = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size --;
        //缩容
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity()/2);
        }
        return set;
    }
    //获取队首元素
    @Override
    public E getFront() {
        if (isEmpty()) {
            throw new IllegalArgumentException("Queue is empty.");
        }
        return data[front];
    }

    //扩容
    public void resize (int catacity) {
        E[] newArray = (E[])new Object[catacity];
        //将旧数组中元素复制到新数组中
        for (int i = 0; i < size; i++) {
            newArray[i] = data[(i + front) % data.length];
        }
        data = newArray;
        front = 0;
        tail = size;
    }

    @Override
    public String toString () {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("LoopQueue");
        stringBuilder.append("front [");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(data[(i + front) % data.length]);
            if (i != size - 1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("] tail");
        return stringBuilder.toString();
    }

 

 

循环队列总结:

 

 

 

 

三.两种队列的简单比较

分别用数组队列和循环队列,将10万个数,入队再出队;

 

 public static Double testQueue (Queue<Integer> queue, int opCount) {
        
        long startTime = System.nanoTime();
        Random random = new Random();
        
        for (int i = 0; i < opCount; i++) {
            queue.enqueue(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            queue.dequeue();
        }

        long endTime = System.nanoTime();
        return ( endTime - startTime) / 1000000000.0;
    }



    public static void main(String[] args) {
        int opCount = 100000;
        Queue<Integer> loopQueue = new LoopQueue<>();
        Queue<Integer> arrayQueue = new ArrayQueue<>();
        System.out.println("ArrayQueue, time1 = " + Main.testQueue(arrayQueue,opCount));
        System.out.println("LoopQueue, time2 = " + Main.testQueue(loopQueue,opCount));

        //new Main().stack();
    }

 

测试结果:两者差距近200倍。

 

 

最后,

浮于表面看千遍,

不如自己思一遍,

希望这篇文章能够对你起到帮助。

 

posted @ 2019-10-29 18:58  inu6  阅读(600)  评论(0编辑  收藏  举报