【LeetCode】循环队列

Java code

通过神器通过神器通过神器通过神器通过神器通过神器

class MyCircularQueue {
    private int front;
    private int rear;
    private int maxSize;
    private int size;
    private int[] arr;
    
    public MyCircularQueue(int k) {
        front = 0;
        rear = 0;
        size = 0;
        maxSize = k;
        arr = new int[maxSize];
    }
    
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        rear = (rear + 1) % maxSize;   
        size++;
        arr[rear] = value;
        return true;
    }
    
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % maxSize;
        size--;
        return true;
    }
    
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return arr[(front + 1) % maxSize];
    }
    
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        return arr[rear];
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public boolean isFull() {
        return size == maxSize;
    }
}
提交记录

【LeetCode】循环队列_第1张图片

你可能感兴趣的:(LeetCode,LeetCode,队列,Java)