1: // Created by Frank M. Carrano and Tim Henry. 2: // Copyright (c) 2013 __Pearson Education__. All rights reserved. 4: /** ADT queue: Circular array-based implementation. 5: Listing 14-5. 6: @file ArrayQueue.cpp */ 8: #include "ArrayQueue.h" // Header file 10: template<class ItemType> 11: ArrayQueue<ItemType>::ArrayQueue() : front(0), back(MAX_QUEUE - 1), count(0) 12: { 13: } // end default constructor 15: template<class ItemType> 16: bool ArrayQueue<ItemType>::isEmpty() const 17: { 18: return count == 0; 19: } // end isEmpty 21: template<class ItemType> 22: bool ArrayQueue<ItemType>::enqueue(const ItemType& newEntry) 23: { 24: bool result = false; 25: if (count < MAX_QUEUE) 26: { 27: // Queue has room for another item 28: back = (back + 1) % MAX_QUEUE; 29: items[back] = newEntry; 30: count++; 31: result = true; 32: } // end if 33: 34: return result; 35: } // end enqueue 37: template<class ItemType> 38: bool ArrayQueue<ItemType>::dequeue() 39: { 40: bool result = false; 41: if (!isEmpty()) 42: { 43: front = (front + 1) % MAX_QUEUE; 44: count--; 45: result = true; 46: } // end if 47: 48: return result; 49: } // end dequeue 51: template<class ItemType> 52: ItemType ArrayQueue<ItemType>::peekFront() const throw(PrecondViolatedExcep) 53: { 54: // Enforce precondition 55: if (isEmpty()) 56: throw PrecondViolatedExcep("peekFront() called with empty queue"); 57: 58: // Queue is not empty; return front 59: return items[front]; 60: } // end peekFront 61: // End of implementation file.