NickAndNick

Queue

Mar 25th, 2020
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Queue {
  5. public:
  6.     Queue() : i_(0U), n_(n), queue_(new int[n_]) {}
  7.     Queue(initializer_list<int> li)
  8.         : i_(li.size()), n_(i_ + n), queue_(new int[n_]) {
  9.         auto beg = queue_;
  10.         for (auto x : li) *beg++ = x;
  11.     }
  12.     Queue(const Queue& q)
  13.         : i_(q.i_), n_(q.n_), queue_(new int[n_]) {
  14.         for (auto i = 0U; i < i_; ++i) queue_[i] = q.queue_[i];
  15.     }
  16.     Queue(Queue&& q) : i_(q.i_), n_(q.n_), queue_(move(q.queue_)) { }
  17.     Queue& operator=(const Queue& q) {
  18.         if (&q != this) {
  19.             i_ = q.i_;
  20.             n_ = q.n_;
  21.             queue_ = new int[n_];
  22.             for (auto i = 0U; i < i_; ++i) queue_[i] = q.queue_[i];
  23.         }
  24.         return *this;
  25.     }
  26.     Queue& operator=(Queue&& q) {
  27.         i_ = q.i_;
  28.         n_ = q.n_;
  29.         queue_ = move(q.queue_);
  30.         return *this;
  31.     }
  32.     ~Queue() {
  33.         if (queue_ != nullptr) {
  34.             delete[] queue_;
  35.             queue_ = nullptr;
  36.         }
  37.     }
  38.     void push(const int value) {
  39.         if (i_ == n_) {
  40.             n_ <<= 1;
  41.             auto tmp = new int[n_];
  42.             for (auto i = 0U; i < i_; ++i) tmp[i] = queue_[i];
  43.             queue_ = move(tmp);
  44.         }
  45.         queue_[i_] = value;
  46.         ++i_;
  47.     }
  48.     void pop() {
  49.         if (i_ == 1U) i_ = 0U;
  50.         else {
  51.             for (auto i = 1U; i < i_; ++i) queue_[i - 1U] = queue_[i];
  52.             --i_;
  53.         }
  54.     }
  55.     int& front() { return *queue_; }
  56.     int& back() { return *(queue_ + i_ - 1); }
  57.     bool empty()const { return 0U == i_; }
  58.     size_t size()const { return i_; }
  59.     size_t capacity()const { return n_; }
  60.     void clear() { i_ = 0U; }
  61. private:
  62.     size_t i_;
  63.     size_t n_;
  64.     int* queue_;
  65.     const static size_t n = 8U;
  66. };
  67. int main() {
  68.     Queue queue({ 1, 2, 3, 4, 5, 6, 7, 8 });
  69.     string ui[] = { "\n - size: ",  "\n - capacity: ", "\n - front: ", "\n - back: " };
  70.     cout << "\tbefore queue.pop():" << ui[0] << queue.size() << ui[1] << queue.capacity();
  71.     if (!queue.empty()) cout << ui[2] << queue.front() << ui[3] << queue.back();
  72.     if (!queue.empty()) queue.pop();
  73.     cout << "\n\tafter queue.pop():" << ui[0] << queue.size() << ui[1] << queue.capacity();
  74.     if (!queue.empty()) cout << ui[2] << queue.front() << ui[3] << queue.back();
  75.     queue.push(1);
  76.     cout << "\n\tafter queue.push(1):" << ui[0] << queue.size() << ui[1] << queue.capacity();
  77.     if (!queue.empty()) cout << ui[2] << queue.front() << ui[3] << queue.back();
  78.     for (auto x = 10; x < 20; ++x) queue.push(x);
  79.     cout << "\n\tafter queue.push(...x):" << ui[0] << queue.size() << ui[1] << queue.capacity();
  80.     if (!queue.empty()) cout << ui[2] << queue.front() << ui[3] << queue.back();
  81.     cout.put('\n');
  82.     cin.get();
  83. }
RAW Paste Data