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
| #include <iostream>
template <typename T> class queue { public: queue(size_t size) : size_(size), front_(0), end_(0) { data_ = new T[size]; } ~queue() { delete[] data_; } queue(const queue& other) = delete; queue(queue&& other) = delete; queue operator=(const queue& other) = delete; queue operator=(queue&& other) = delete;
bool is_empty() { return front_ == end_; } bool is_full() { return front_ = (end_ + 1) % size_; }
const T& front() const { return data_[front_]; }
void push(const T& val) { if ((end_ + 1) % size_ != front_) { data_[end_] = val; end_ = (end_ + 1) % size_; } }
void pop() { if (front_ != end_) { front_ = (front_ + 1) % size_; } }
private: size_t front_; size_t end_; size_t size_; T* data_; };
int main() { queue<int> q(5); q.push(1); q.push(2); q.push(3); q.push(4); q.push(5); q.pop(); q.pop(); q.push(6); q.push(7); q.push(8); while (!q.is_empty()) { std::cout << q.front() << std::endl; q.pop(); } return 0; }
|