-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.cpp
69 lines (53 loc) · 1.21 KB
/
deque.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
60
61
62
63
64
65
66
67
68
69
#include<cassert>
#include<iostream>
#include<vector>
using namespace std;
template <class T>
class Deque {
private:
vector<T> data_;
int size_;
int head_;
int tail_;
int capacity_;
public:
Deque(int size) : size_(0), head_(0), tail_(size-1), capacity_(size), data_(size) {}
void push_front(T v) {
assert(head_ <= tail_);
data_[head_++] = v;
size_++;
}
void push_back(T v) {
assert(head_ <= tail_);
data_[tail_--] = v;
size_++;
}
void pop_front() {
assert(head_ > 0);
head_--;
size_--;
}
void pop_back() {
assert(tail_ < capacity_);
tail_++;
size_--;
}
T front() const {
assert(head_ > 0);
return data_[head_-1];
}
T back() const {
assert(tail_ < capacity_-1);
return data_[tail_+1];
}
int size() const { return size_; }
int empty() const { return size_ == 0; }
~Deque() {}
};
int main() {
Deque<int> mydeque(3);
mydeque.push_front(1);
mydeque.push_back(3);
cout << mydeque.front() << " " << mydeque.back() << endl;
cout << mydeque.size();
}