-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.h
92 lines (81 loc) · 1.93 KB
/
Deque.h
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
83
84
85
86
87
88
89
90
91
92
template <typename T>
class Deque {
public:
Deque() : _size(0), _front(0), _back(0), _capacity(4), _data(new T[4]) {}
void push_back(const T& value) {
if (_size == _capacity) {
_resize(_capacity * 2);
}
_data[_back] = value;
_back = (_back + 1) % _capacity;
++_size;
}
void push_front(const T& value) {
if (_size == _capacity) {
_resize(_capacity * 2);
}
_front = (_front - 1 + _capacity) % _capacity;
_data[_front] = value;
++_size;
}
void pop_back() {
if (_size == 0) {
// handle underflow error
} else {
_back = (_back - 1 + _capacity) % _capacity;
--_size;
if (_size < _capacity / 4) {
_resize(_capacity / 2);
}
}
}
void pop_front() {
if (_size == 0) {
// handle underflow error
} else {
_front = (_front + 1) % _capacity;
--_size;
if (_size < _capacity / 4) {
_resize(_capacity / 2);
}
}
}
size_t size() const {
return _size;
}
bool empty() const {
return _size == 0;
}
void clear() {
_size = 0;
_front = 0;
_back = 0;
}
T& operator[](size_t index) {
if (index < _size) {
return _data[(_front + index) % _capacity];
} else {
// handle out of bounds error
// you can throw an exception or return a default value
// for example, return the last element
return _data[(_front + _size - 1) % _capacity];
}
}
private:
size_t _size;
size_t _front;
size_t _back;
size_t _capacity;
T* _data;
void _resize(size_t newCapacity) {
T* newData = new T[newCapacity];
for (size_t i = 0; i < _size; ++i) {
newData[i] = _data[(_front + i) % _capacity];
}
_capacity = newCapacity;
_front = 0;
_back = _size;
delete[] _data;
_data = newData;
}
};