2024每日刷题(128)
Leetcode—622. 设计循环队列
实现代码
class MyCircularQueue {
public:
MyCircularQueue(int k): q(k) {
qSize = k;
}
bool enQueue(int value) {
if(isFull()) {
return false;
}
q[rear] = value;
rear = (rear + 1) % qSize;
deflag = false;
enflag = true;
return true;
}
bool deQueue() {
if(isEmpty()) {
return false;
}
front = (front + 1) % qSize;
deflag = true;
enflag = false;
return true;
}
int Front() {
if(isEmpty()) {
return -1;
}
return q[front];
}
int Rear() {
if(isEmpty()) {
return -1;
}
return q[(rear - 1 + qSize) % qSize];
}
bool isEmpty() {
if(deflag && front == rear) {
return true;
}
if(enflag == false && rear == 0 && deflag == false) {
return true;
}
return false;
}
bool isFull() {
if(enflag && front == rear) {
return true;
}
return false;
}
private:
vector<int> q;
int qSize = -1;
int front = 0;
int rear = 0;
int enflag = false;
int deflag = false;
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue* obj = new MyCircularQueue(k);
* bool param_1 = obj->enQueue(value);
* bool param_2 = obj->deQueue();
* int param_3 = obj->Front();
* int param_4 = obj->Rear();
* bool param_5 = obj->isEmpty();
* bool param_6 = obj->isFull();
*/
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!