// queue implemented as an array #ifndef POINTER_QUEUE #define POINTER_QUEUE #include template class QueueNode { public: T data; QueueNode *next; QueueNode(const T& el) : data(el), next(nullptr) {} }; class QueueEmptyException : public std::exception { public: QueueEmptyException() noexcept {} const char* what() const { return "Queue is empty."; } }; template class PointerQueue { public: PointerQueue() { front = back = nullptr; } PointerQueue(const PointerQueue& other) = delete; PointerQueue(PointerQueue&& other) = delete; ~PointerQueue() { while(front != nullptr) { QueueNode* toDelete = front; front = toDelete->next; delete toDelete; } } PointerQueue& operator=(const PointerQueue& other) = delete; PointerQueue& operator=(PointerQueue&& other) = delete; void enqueue(T); T dequeue(); bool isEmpty() { return front == nullptr; } private: QueueNode *front, *back; }; template void PointerQueue::enqueue(T el) { if(front == nullptr) front = back = new QueueNode(el); else { back->next = new QueueNode(el); back = back->next; } } template T PointerQueue::dequeue() { if(front == nullptr) throw QueueEmptyException(); QueueNode* toRemove = front; front = front->next; T data = toRemove->data; delete toRemove; return data; } #endif