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