Leetcode_622_设计循环队列_数据结构

class MyCircularQueue {

    int[] array;
    // 闭区间
    int   start;
    // 开区间
    int   end;
    int   len;

    public MyCircularQueue(int k) {
        array = new int[k];
        start = end = len = 0;
    }

    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        len++;
        array[end % array.length] = value;
        end = (end + 1) % array.length;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        len--;
        start = (start + 1) % array.length;
        return true;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return array[start];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        return array[(end - 1 + array.length) % array.length];
    }

    public boolean isEmpty() {
        return len == 0;
    }

    public boolean isFull() {
        return len == array.length;
    }
}

你可能感兴趣的:(daily_algorithm,数据结构,leetcode,数据结构,算法)