LeetCode笔记 622. 设计循环队列
作者:神秘网友
发布时间:2020-09-22 07:03:59
LeetCode笔记 622. 设计循环队列
LeetCode笔记 622. 设计循环队列class MyCircularQueue { private: vector <int> data; int head; int tail; int count; int size; public: /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) { data.resize(k); head = 0; tail = -1; count = 0; size = k; } /** Insert an element into the circular queue. Return true if the operation is successful. */ bool enQueue(int value) { if (isFull()){ return false; } tail = (tail + 1) % size; data[tail] = value; count++; return true; } /** Delete an element from the circular queue. Return true if the operation is successful. */ bool deQueue() { if (isEmpty()){ return false; } head++; count--; return true; } /** Get the front item from the queue. */ int Front() { if (isEmpty()) { return -1; } return data[head]; } /** Get the last item from the queue. */ int Rear() { if (tail < 0 && tail >= size) { return -1; } if (isEmpty()) { return -1; } return data[tail]; } /** Checks whether the circular queue is empty or not. */ bool isEmpty() { return (count == 0); } /** Checks whether the circular queue is full or not. */ bool isFull() { return (count == size); } };