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());
        }
    }
  • 相关阅读:
    理解python可变类型vs不可变类型,深拷贝vs浅拷贝
    在centos上安装mysql5.7的三种方法
    使用python脚本实现基于指定字符串的文本排序
    在CentOS 7上安装Python3.5源码包
    SVN入门使用
    Linux-vim命令
    在博客园里使用百度统计
    个人网站类型的运维
    apache解析多个域名
    使用php来访问操作sql server
  • 原文地址:https://www.cnblogs.com/hetaoyuan/p/12286325.html
Copyright © 2011-2022 走看看