Data structure queue Queue_JAVA (ArrayQueue based on the running speed comparison of array and LoopQueue circular queue)

Precautions

No.1

data = (E[]) new Object[Capacity+1];Brackets

No.2

if(index < 0 || index >= size)
    throw new IllegalArgumentException("Set failed. Index is illegal.");

Pay attention to the format of the exception thrown

No.3

//进队enqueue片段
		data[tail] = e;
        tail=(tail+1)% data.length;
        size++;
//出队dequeue片段
		E temp = data[front];
        data[front]=null;
        front=(front+1)% data.length;
        size--;

The front and tail
queues of the circular queue are empty: front==tail
queue is full: (tail+1)% data.length

No.4

Pay attention to the format

//重写toString方法片段
StringBuilder res = new StringBuilder();
        res.append(String.format("LoopQueue : size = %d , capacity = %d\n", size, getCapacity()));

Queue interface (source code)

public interface Queue<E> {
    
    
    int getSize();
    boolean isEmpty();
    int getCapacity();
    E dequeue();
    E getFront();
    void enqueue(E e);
}

ArrayQueue (source code)

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

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

    public  ArrayQueue(){
    
    
        array = new Array<>();
    }
    @Override
    public int getSize() {
    
    
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
    
    
        return array.isEmpty();
    }

    @Override
    public int getCapacity() {
    
    
        return array.getCapacity();
    }

    @Override
    public E getFront() {
    
    
        return array.get(0);
    }

    @Override
    public E dequeue() {
    
    
        return array.delete(0);
    }

    @Override
    public void enqueue(E e) {
    
    
        array.addLast(e);
    }

    @Override
    public String toString(){
    
    
        StringBuilder res = new StringBuilder();
        res.append("Queue:front ");
        res.append("[");
        for(int i = 0 ; i < array.getSize(); i ++){
    
    
            res.append(array.get(i));
            if(i!= array.getSize()-1)
                res.append(", ");
        }
        res.append("] tail");
        return res.toString();
    }

}

LoopQueue (source code)

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);
    }

    @Override
    public int getSize() {
    
    
        return size;
    }

    @Override
    public boolean isEmpty() {
    
    
        return front == tail;
    }

    @Override
    public int getCapacity() {
    
    
        return data.length-1;
    }

    @Override
    public E dequeue() {
    
    
        if(isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        E temp = data[front];
        data[front]=null;
        front=(front+1)% data.length;
        size--;
        if(size == getCapacity() / 4 && getCapacity() / 2 != 0)
            resize(getCapacity() / 2);
        return temp;
    }

    @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 getFront() {
    
    
        if(isEmpty())
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        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("LoopQueue : 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();
    }
}

main function

ArrayQueue test main function

    public static void main(String[] args){
    
    

        ArrayQueue<Integer> queue = new ArrayQueue<>();
        for(int i = 0 ; i < 10 ; i ++){
    
    
            queue.enqueue(i);
            System.out.println(queue);

            if(i % 3 == 2){
    
    
                queue.dequeue();
                System.out.println(queue);
            }
        }
    }

LoopQueue test main function

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

Test the difference between the two

Source code

import java.util.Random;

public class Main {
    
    

    // 测试使用q运行opCount个enqueueu和dequeue操作所需要的时间,单位:秒
    private static double testQueue(Queue<Integer> q, int opCount){
    
    

        long startTime = System.nanoTime();

        Random random = new Random();
        for(int i = 0 ; i < opCount ; i ++)
            q.enqueue(random.nextInt(Integer.MAX_VALUE));
        for(int i = 0 ; i < opCount ; i ++)
            q.dequeue();

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

    public static void main(String[] args) {
    
    

        int opCount = 100000;

        ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
        double time1 = testQueue(arrayQueue, opCount);
        System.out.println("ArrayQueue, time: " + time1 + " s");

        LoopQueue<Integer> loopQueue = new LoopQueue<>();
        double time2 = testQueue(loopQueue, opCount);
        System.out.println("LoopQueue, time: " + time2 + " s");
    }
}

result

"C:\Program Files\Java\jdk14\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2\lib\idea_rt.jar=56799:C:\Program Files\JetBrains\IntelliJ IDEA 2020.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\12778\IdeaProjects\first\out\production\Array com.Queue.main
ArrayQueue, time: 53.0073206 s
LoopQueue, time: 0.0352907 s

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/TOPic666/article/details/107711907