zoukankan      html  css  js  c++  java
  • 循环队列

    假设循环队列最多能容纳k个整型数字,那么我们需要开辟k+1个空间,如图,当k = 6的时候,空间大小为7,即array.length() = 7.在起始的时候,front = rear = 0;在每次添加数字的时候( enqueue() ),rear都会+1,而k = 6,也就是从初始位置rear = 0时,最多一直连续添加6次,此时 rear = 6,无法再次添加,那么要想再次添加,只有通过出队列(dequeue()),每出一次队列,front的值都会+1,最多加到front = 6,此时,队列为空,那么rear和front的值都为6,再次入队列,rear会重新从0开始计算,再次出队列,front也会从0开始计算。每次删除的时候,从(front+1)/array.lenth()的位置删,每次添加的时候从(rear+1)/array.length()的位置添加.

    class MyCircularQueue {
    
        private int front;//队列头
        private int rear;//队列尾
        private int usedSize;//数据个数
        private int[] elem;//数组
    
    
        /** Initialize your data structure here.
         * Set the size of the queue to be k. */
        public MyCircularQueue(int k) {
            this.elem = new int[k+1];
            this.front = 0;
            this.rear = 0;
            this.usedSize = 0;
        }
    
        /** Insert an element into the circular queue.
         * Return true if the operation is successful. */
        public boolean enQueue(int value) {
            if(isFull()){
                return false;
            }
            this.elem[this.rear] = value;
            this.rear = (this.rear+1)%this.elem.length;
            this.usedSize++;
            return true;
        }
    
        /** Delete an element from the circular queue.
         * Return true if the operation is successful. */
        public boolean deQueue() {
            if(isEmpty()){
                return false;
            }
            this.front = (this.front+1)%this.elem.length;
            this.usedSize--;
            return true;
        }
    
        /** Get the front item from the queue. */
        public int Front() {
            if(isEmpty()){
                return -1;
            }
            return this.elem[this.front];
        }
    
        /** Get the last item from the queue. */
        public int Rear() {
            if(isEmpty()){
                return -1;
            }
            int index = this.rear == 0 ? this.elem.length-1 : this.rear-1;
            return this.elem[index];
        }
    
        /** Checks whether the circular queue is empty or not. */
        public boolean isEmpty() {
            return this.rear == this.front;
        }
    
        /** Checks whether the circular queue is full or not. */
        public boolean isFull() {
            if((this.rear+1)%this.elem.length == this.front){
                return true;
            }
            return false;
        }
    }
    
    class Main1{
        public static void main(String[] args) {
           MyCircularQueue myCircularQueue = new MyCircularQueue(6);
            for (int i = 1; i <=10; i++) {
                myCircularQueue.enQueue(i);
            }
            for (int i = 1; i <= 4; i++) {
                myCircularQueue.deQueue();
            }
            for (int i = 1; i <= 3; i++) {
                myCircularQueue.enQueue(i);
            }
            for (int i = 1; i <=3 ; i++) {
                myCircularQueue.deQueue();
            }
            for (int i = 1; i <=3 ; i++) {
                myCircularQueue.enQueue(i);
            }
            System.out.println(myCircularQueue.Front());
        }
    }
  • 相关阅读:
    visual studio code 中文
    vue中常用插件(货币、日期)
    PS与CSS字间距转换
    常用css样式(文字超出部分用省略号显示、鼠标经过图片放大、出现阴影)
    swiper在一个页面多个轮播图
    git上传项目
    Win10下安装SVN出现2503/2502解决方法
    关于yii2学习笔记:gii的使用
    nginx反向代理解决跨域
    树莓派4安装centos
  • 原文地址:https://www.cnblogs.com/hetaoyuan/p/12286325.html
Copyright © 2011-2022 走看看