美文网首页
写一个循环队列

写一个循环队列

作者: xin激流勇进 | 来源:发表于2019-04-03 16:10 被阅读0次
    image.png
    package structures;
    
    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 int getSize() {
            return size;
        }
    
        @Override
        public E getFront() {
            if (isEmpty()) {
                throw new IllegalArgumentException("the queue is empty");
            }
    
            return data[front];
        }
    
        @Override
        public boolean isEmpty() {
            return front == tail;
        }
    
        @Override
        public void enqueue(E e) {
            //判断队列是否满了
            if ((tail + 1) % data.length == front) {
                resize(getCapacity() * 2);
            }
    
            data[tail] = e;
            tail = (tail + 1) % data.length;
            size ++;
        }
    
        private void resize(int capacity) {
            E[] newData = (E[]) new Object[capacity + 1];
    
            for (int i = 0; i < size; i++) {
                newData[i] = data[(i + front) % data.length];
            }
    
            data = newData;
            front = 0;
            tail = size;
        }
    
        @Override
        public E dequeue() {
            if (isEmpty()) {
                throw new IllegalArgumentException("queue is empty");
            }
    
            E res = data[front];
            front = (front + 1) % data.length;
            size --;
    
            if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
                resize(getCapacity() / 2);
            }
    
            return res;
        }
    
        @Override
        public String toString() {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(String.format("LoopQueue: size = %d\ncapacity = %d\n", size, getCapacity()));
            stringBuffer.append("front [");
            for (int i = front; i != tail; i = (i + 1) % data.length) {
                stringBuffer.append(data[i]);
                if (tail != (i + 1) % data.length) {
                    stringBuffer.append(',');
                }
            }
    
            stringBuffer.append("] tail\n");
    
    
            return stringBuffer.toString();
        }
    }
    
    

    测试效率

    package structures;
    
    import java.util.Random;
    
    public class Main {
        public static void main(String[] args) {
            LoopQueue<Integer> loopQueue = new LoopQueue<>();
            ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
    
            double test1 = test(loopQueue, 100000);
            double test2 = test(arrayQueue, 100000);
    
            System.out.println(test1);
            System.out.println(test2);
        }
    
        public static double test(Queue<Integer> q, int count) {
            long start = System.nanoTime();
            Random random = new Random();
            for (int i = 0; i < count; i++) {
                q.enqueue(random.nextInt(Integer.MAX_VALUE));
            }
    
            for (int i = 0; i < count; i++) {
                q.dequeue();
            }
    
            long stop = System.nanoTime();
    
            return (stop - start) / 1000000000.0;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:写一个循环队列

          本文链接:https://www.haomeiwen.com/subject/znyzbqtx.html