-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCircularDeque.cpp
More file actions
82 lines (73 loc) · 1.89 KB
/
MyCircularDeque.cpp
File metadata and controls
82 lines (73 loc) · 1.89 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class MyCircularDeque {
private:
int _max_size, _head = 0, _tail = -1, _size = 0;
vector<int> _data;
public:
MyCircularDeque(int k): _max_size(k), _data(k) {}
bool insertFront(int value) {
if (_size == _max_size)
return false;
if (!_size) {
_data[0] = value;
_head = _tail = 0;
}
else {
_head = (_head) ? _head - 1 : _max_size - 1;
_data[_head] = value;
}
++_size;
return true;
}
bool insertLast(int value) {
if (_size == _max_size)
return false;
if (!_size) {
_data[0] = value;
_head = _tail = 0;
}
else {
_tail = (_tail == _max_size - 1) ? 0 : _tail + 1;
_data[_tail] = value;
}
++_size;
return true;
}
bool deleteFront() {
if (!_size) return false;
_head = (_head == _max_size - 1) ? 0 : _head + 1;
--_size;
return true;
}
bool deleteLast() {
if (!_size) return false;
_tail = (_tail == 0) ? _max_size - 1 : _tail - 1;
--_size;
return true;
}
int getFront() {
if (!_size) return -1;
return _data[_head];
}
int getRear() {
if (!_size) return -1;
return _data[_tail];
}
bool isEmpty() {
return !_size;
}
bool isFull() {
return _size == _max_size;
}
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque* obj = new MyCircularDeque(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/