congr / world

2 stars 1 forks source link

LeetCode : 622. Design Circular Queue #496

Closed congr closed 5 years ago

congr commented 5 years ago

https://leetcode.com/problems/design-circular-queue/

image image

congr commented 5 years ago
class MyCircularQueue {

    List<Integer> list;
    int capa;

    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        list = new LinkedList();    
        this.capa = k;
    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if (isFull()) return false;
        list.add(value);
        return true;
    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if (isEmpty()) return false;    
        list.remove(0);
        return true;
    }

    /** Get the front item from the queue. */
    public int Front() {
        if (isEmpty()) return -1;
        return list.get(0);
    }

    /** Get the last item from the queue. */
    public int Rear() {
        if (isEmpty()) return -1;
        return list.get(list.size()-1);
    }

    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return list.isEmpty();
    }

    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        return list.size() == capa;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */