leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0622.cpp (797B)


0 class MyCircularQueue {
1 public:
2 MyCircularQueue(int k) : n(k), q(k) {}
4 bool enQueue(int value) {
5 if (isFull()) return false;
7 q[back++] = value;
8 if (back >= n) back -= n;
9 size++;
11 return true;
12 }
14 bool deQueue() {
15 if (isEmpty()) return false;
17 if (++front >= n) front -= n;
18 size--;
20 return true;
21 }
23 int Front() const {
24 if (isEmpty()) return -1;
25 return q[front];
26 }
28 int Rear() const {
29 if (isEmpty()) return -1;
30 if (back == 0) return q.back();
31 return q[back - 1];
32 }
34 bool isEmpty() const { return size == 0; }
36 bool isFull() const { return size == n; }
38 const int n;
39 vector<int> q;
41 int front = 0, back = 0;
42 int size = 0;
43 };