码迷,mamicode.com
首页 > 其他好文 > 详细

循环队列

时间:2020-02-09 11:41:44      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:rcu   假设   stat   eth   队列   false   连续   into   数组   

 

假设循环队列最多能容纳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());
    }
}

 

循环队列

标签:rcu   假设   stat   eth   队列   false   连续   into   数组   

原文地址:https://www.cnblogs.com/hetaoyuan/p/12286325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!