• 队列01--[队列&双端队列&循环队列&双端循环队列]


    1.队列

    1.1队列简介

     1.2队列的接口设计

    import com.mj.list.LinkedList;
    import com.mj.list.List;
    
    public class Queue<E> {
        private List<E> list = new LinkedList<>();
        
        public int size() {
            return list.size();
        }
    
        public boolean isEmpty() {
            return list.isEmpty();
        }
        
        public void clear() {
            list.clear();
        }
    
        public void enQueue(E element) {
            list.add(element);
        }
    
        public E deQueue() {
            return list.remove(0);
        }
    
        public E front() {
            return list.get(0);
        }
    }
    View Code

    1.3练习-用栈实现队列

    2.双端队列

    2.1简介

    import com.mj.list.LinkedList;
    import com.mj.list.List;
    
    public class Deque<E> {
        private List<E> list = new LinkedList<>();
        
        public int size() {
            return list.size();
        }
    
        public boolean isEmpty() {
            return list.isEmpty();
        }
        
        public void clear() {
            list.clear();
        }
    
        public void enQueueRear(E element) {
            list.add(element);
        }
    
        public E deQueueFront() {
            return list.remove(0);
        }
    
        public void enQueueFront(E element) {
            list.add(0, element);
        }
    
        public E deQueueRear() {
            return list.remove(list.size() - 1);
        }
    
        public E front() {
            return list.get(0);
        }
    
        public E rear() {
            return list.get(list.size() - 1);
        }
    }
    View Code

    3.循环队列

    3.1简介

     3.2接口设计

    @SuppressWarnings("unchecked")
    public class CircleQueue<E> {
        private int front;
        private int size;
        private E[] elements;
        private static final int DEFAULT_CAPACITY = 10;
        
        public CircleQueue() {
            elements = (E[]) new Object[DEFAULT_CAPACITY];
        }
        
        public int size() {
            return size;
        }
    
        public boolean isEmpty() {
            return size == 0;
        }
        
        public void clear() {
            for (int i = 0; i < size; i++) {
                elements[index(i)] = null;
            }
            front = 0;
            size = 0;
        }
    
        public void enQueue(E element) {
            ensureCapacity(size + 1);
            
            elements[index(size)] = element;
            size++;
        }
    
        public E deQueue() {
            E frontElement = elements[front];
            elements[front] = null;
            front = index(1);
            size--;
            return frontElement;
        }
    
        public E front() {
            return elements[front];
        }
        
        @Override
        public String toString() {
            StringBuilder string = new StringBuilder();
            string.append("capcacity=").append(elements.length)
            .append(" size=").append(size)
            .append(" front=").append(front)
            .append(", [");
            for (int i = 0; i < elements.length; i++) {
                if (i != 0) {
                    string.append(", ");
                }
                
                string.append(elements[i]);
            }
            string.append("]");
            return string.toString();
        }
        //索引转换
        //把循环队列里面0 1 2 3这样固定不变的索引变成在循环队列里面真实的索引
        private int index(int index) {
            index += front;
            return index - (index >= elements.length ? elements.length : 0);
        }
        
        /**
         * 保证要有capacity的容量
         * @param capacity
         */
        private void ensureCapacity(int capacity) {
            int oldCapacity = elements.length;
            if (oldCapacity >= capacity) return;
            
            // 新容量为旧容量的1.5倍
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            E[] newElements = (E[]) new Object[newCapacity];
            for (int i = 0; i < size; i++) {
                newElements[i] = elements[index(i)];
            }
            elements = newElements;
            
            // 重置front
            front = 0;
        }
    }
    View Code

    4.循环双端队列

    4.1接口设计

    @SuppressWarnings("unchecked")
    public class CircleDeque<E> {
        private int front;
        private int size;
        private E[] elements;
        private static final int DEFAULT_CAPACITY = 10;
        
        public CircleDeque() {
            elements = (E[]) new Object[DEFAULT_CAPACITY];
        }
        
        public int size() {
            return size;
        }
    
        public boolean isEmpty() {
            return size == 0;
        }
    
        public void clear() {
            for (int i = 0; i < size; i++) {
                elements[index(i)] = null;
            }
            front = 0;
            size = 0;
        }
    
        /**
         * 从尾部入队
         * @param element
         */
        public void enQueueRear(E element) {
            ensureCapacity(size + 1);
            
            elements[index(size)] = element;
            size++;
        }
    
        /**
         * 从头部出队
         * @param element
         */
        public E deQueueFront() {
            E frontElement = elements[front];
            elements[front] = null;
            front = index(1);
            size--;
            return frontElement;
        }
    
        /**
         * 从头部入队
         * @param element
         */
        public void enQueueFront(E element) {
            ensureCapacity(size + 1);
            
            front = index(-1);
            elements[front] = element;
            size++;
        }
    
        /**
         * 从尾部出队
         * @param element
         */
        public E deQueueRear() {
            int rearIndex = index(size - 1);
            E rear = elements[rearIndex];
            elements[rearIndex] = null;
            size--;
            return rear;
        }
    
        public E front() {
            return elements[front];
        }
    
        public E rear() {
            return elements[index(size - 1)];
        }
    
        @Override
        public String toString() {
            StringBuilder string = new StringBuilder();
            string.append("capcacity=").append(elements.length)
            .append(" size=").append(size)
            .append(" front=").append(front)
            .append(", [");
            for (int i = 0; i < elements.length; i++) {
                if (i != 0) {
                    string.append(", ");
                }
                
                string.append(elements[i]);
            }
            string.append("]");
            return string.toString();
        }
        
        private int index(int index) {
            index += front;
            if (index < 0) {
                return index + elements.length;
            }
            return index - (index >= elements.length ? elements.length : 0);
        }
        
        /**
         * 保证要有capacity的容量
         * @param capacity
         */
        private void ensureCapacity(int capacity) {
            int oldCapacity = elements.length;
            if (oldCapacity >= capacity) return;
            
            // 新容量为旧容量的1.5倍
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            E[] newElements = (E[]) new Object[newCapacity];
            for (int i = 0; i < size; i++) {
                newElements[i] = elements[index(i)];
            }
            elements = newElements;
            
            // 重置front
            front = 0;
        }
    }
    View Code

     5.优化思路

  • 相关阅读:
    大数据产品对比
    3人3天3桶水9个人9天用几桶水
    skatebroads
    手机全面屏屏占比93%以上解决方案
    POC
    公司网站 解决方案 案例
    GBT 31000-2015 社会治安综合治理基础数据规范 数据项 编码
    GBT 33200-2016 社会治安综合治理 综治中心建设与管理规范 GBT 31000-2015 社会治安综合治理基础数据规范
    大数据 交警 户外广告设施管理监管系统平台高空坠物智慧社区城市城管警务
    破解爱奇艺腾讯优酷会员限制
  • 原文地址:https://www.cnblogs.com/ggnbnb/p/12435479.html
Copyright © 2020-2023  润新知