-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0622 - Design Circular Queue.cpp
59 lines (49 loc) · 1.04 KB
/
0622 - Design Circular Queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <vector>
class MyCircularQueue
{
std::vector<int> _data;
size_t _i{0}, _n{0};
const size_t _N;
public:
MyCircularQueue(int k) : _N(k), _data(k) {}
bool enQueue(int value)
{
if (_n == _N) return false;
_i = (_i + _N + 1) % _N;
_data[_i] = value;
_n++;
return true;
}
bool deQueue()
{
if (_n == 0) return false;
_n--;
return true;
}
int Rear()
{
return (_n > 0) ? _data[ _i ] : -1;
}
int Front()
{
return (_n > 0) ? _data[ (_i + _N - _n + 1) % _N ] : -1;
}
bool isEmpty()
{
return _n == 0;
}
bool isFull()
{
return _n == _N;
}
};
/**
* 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();
*/