1: // Created by Frank M. Carrano and Timothy M. Henry. 2: // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. 4: // Listing 17-4. 6: /** Heap-based implementation of the ADT priority queue. 7: @file HeapPriorityQueue.cpp */ 9: #include "HeapPriorityQueue.h" 11: template<class ItemType> 12: HeapPriorityQueue<ItemType>::HeapPriorityQueue() 13: { 14: ArrayMaxHeap<ItemType>(); 15: } // end constructor 17: template<class ItemType> 18: bool HeapPriorityQueue<ItemType>::isEmpty() const 19: { 20: return ArrayMaxHeap<ItemType>::isEmpty(); 21: } // end isEmpty 23: template<class ItemType> 24: bool HeapPriorityQueue<ItemType>::enqueue(const ItemType& newEntry) 25: { 26: return ArrayMaxHeap<ItemType>::add(newEntry); 27: } // end enqueue 29: template<class ItemType> 30: bool HeapPriorityQueue<ItemType>::dequeue() 31: { 32: return ArrayMaxHeap<ItemType>::remove(); 33: } // end dequeue 35: template<class ItemType> 36: ItemType HeapPriorityQueue<ItemType>::peekFront() const throw(PrecondViolatedExcept) 37: { 38: try 39: { 40: return ArrayMaxHeap<ItemType>::peekTop(); 41: } 42: catch (PrecondViolatedExcept e) 43: { 44: throw PrecondViolatedExcept("Attempted peek into an empty priority queue."); 45: } // end try/catch 46: } // end peekFront