ifndef QUEUE_HDR #define QUEUE_HDR #include using namespace std; template class Queue { private: T * arr; int capacity; int count; int whereToPut; int whereToGet; void Resize(); void CopyFrom(const Queue& other); void Free(); public: Queue(); Queue(const Queue& other); Queue& operator=(const Queue& other); ~Queue(); bool isEmpty() const; void Push(T el); T Pop(); T& Peek(); }; template void Queue::Resize() { T* temp = new T[capacity * 2]; for (int i = 0; i < capacity; ++i) temp[i] = Pop(); whereToGet = 0; whereToPut = capacity; count = capacity; capacity *= 2; delete[] arr; arr = temp; } template void Queue::CopyFrom(const Queue& other) { arr = new T[other.capacity]; for (int i = 0; i < other.count; ++i) arr[i] = other.arr[i]; count = other.count; capacity = other.capacity; whereToPut = other.whereToPut; whereToGet = other.whereToGet; } template void Queue::Free() { delete[] arr; count = capacity = 0; } template Queue::Queue() { arr = new T[4]; capacity = 4; count = 0; whereToPut = 0; whereToGet = 0; } template Queue::Queue(const Queue& other) { CopyFrom(other); } template Queue& Queue::operator=(const Queue& other) { if (this != &other) { Free(); CopyFrom(other); } return *this; } template Queue::~Queue() { Free(); } template bool Queue::isEmpty() const { return count == 0; } template void Queue::Push(T el) { if (count == capacity) Resize(); arr[whereToPut] = el; (whereToPut += 1) %= capacity; count++; } template T Queue::Pop() { if (count == 0) throw "Queue is empty"; T elToReturn = arr[whereToGet]; count--; (whereToGet += 1) %= capacity; return elToReturn; } template T& Queue::Peek() { if (isEmpty()) throw std::logic_error("empty queue"); return arr[whereToGet]; } #endif // !QUEUE_HDR