// From the software distribution accompanying the textbook // "A Practical Introduction to Data Structures and Algorithm Analysis, // Third Edition (C++)" by Clifford A. Shaffer. // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer. // This is the file to include in your code if you want access to the // complete AQueue template class // First, get the declaration for the base stack class #include "queue.h" // This is the declaration for AStack. // Array-based queue implementation template class AQueue: public Queue { private: int maxSize; // Maximum size of queue int front; // Index of front element int rear; // Index of rear element E *listArray; // Array holding queue elements public: AQueue(int size =defaultSize) { // Constructor // Make list array one position larger for empty slot maxSize = size+1; rear = 0; front = 1; listArray = new E[maxSize]; } ~AQueue() { delete [] listArray; } // Destructor void clear() { rear = 0; front = 1; } // Reinitialize void enqueue(const E& it) { // Put "it" in queue Assert(((rear+2) % maxSize) != front, "Queue is full"); rear = (rear+1) % maxSize; // Circular increment listArray[rear] = it; } E dequeue() { // Take element out Assert(length() != 0, "Queue is empty"); E it = listArray[front]; front = (front+1) % maxSize; // Circular increment return it; } const E& frontValue() const { // Get front value Assert(length() != 0, "Queue is empty"); return listArray[front]; } virtual int length() const // Return length { return ((rear+maxSize) - front + 1) % maxSize; } };