1: // Created by Frank M. Carrano and Timothy M. Henry.
2: // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
4: /** Listing 7-2
5: @file ArrayStack.cpp */
6: #include <cassert> // For assert
7: #include "ArrayStack.h" // Header file
9: template<class ItemType>
10: ArrayStack<ItemType>::ArrayStack() : top(-1)
11: {
12: } // end default constructor
14: // Copy constructor and destructor are supplied by the compiler
16: template<class ItemType>
17: bool ArrayStack<ItemType>::isEmpty() const
18: {
19: return top < 0;
20: } // end isEmpty
22: template<class ItemType>
23: bool ArrayStack<ItemType>::push(const ItemType& newEntry)
24: {
25: bool result = false;
26: if (top < DEFAULT_CAPACITY - 1) // Does stack have room for newEntry?
27: {
28: top++;
29: items[top] = newEntry;
30: result = true;
31: } // end if
32:
33: return result;
34: } // end push
36: template<class ItemType>
37: bool ArrayStack<ItemType>::pop()
38: {
39: bool result = false;
40: if (!isEmpty())
41: {
42: top--;
43: result = true;
44: } // end if
45:
46: return result;
47: } // end pop
49: template<class ItemType>
50: ItemType ArrayStack<ItemType>::peek() const
51: {
52: assert(!isEmpty()); // Enforce precondition during debugging
53:
54: // Stack is not empty; return top
55: return items[top];
56: } // end peek
57: // End of implementation file.