// ******************************************************** // Implementation file QueueA.cpp for the ADT queue. // Circular array-based implementation. // The array has indexes to the front and back of the // queue. A counter tracks the number of items currently // in the queue. // ******************************************************** #include "QueueA.h" // header file Queue::Queue():front(0), count(0) { } // end default constructor Queue::~Queue() { dequeueAll(); } bool Queue::isEmpty() const { return (count == 0); } // end isEmpty void Queue::enqueue(Object* newItem) throw(QueueException) { if (count == MAX_QUEUE) throw QueueException("QueueException: queue full on enqueue"); else { // queue is not full; insert item items[(front+count) % MAX_QUEUE] = newItem; ++count; } // end if } // end enqueue Object* Queue::dequeue() throw(QueueException) { if (isEmpty()) throw QueueException("QueueException: empty queue, cannot dequeue"); else { // queue is not empty; remove front Object* toBeReturned = items[front]; front = (front+1) % MAX_QUEUE; --count; return toBeReturned; } // end if } // end dequeue void Queue::dequeueAll() { while (!isEmpty()) { // queue is not empty; delete and remove front element delete items[front]; front = (front+1) % MAX_QUEUE; --count; } // end if } // end dequeue const Object* Queue::peek() const throw(QueueException) { if (isEmpty()) throw QueueException("QueueException: empty queue, cannot getFront"); else // queue is not empty; retrieve front return items[front]; } // end getFront // End of implementation file.