1: // Created by Frank M. Carrano and Tim Henry.
2: // Copyright (c) 2013 __Pearson Education__. All rights reserved.
4: /** Listing 7-1
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 < MAX_STACK - 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
37: template<class ItemType>
38: bool ArrayStack<ItemType>::pop()
39: {
40: bool result = false;
41: if (!isEmpty())
42: {
43: top--;
44: result = true;
45: } // end if
46:
47: return result;
48: } // end pop
51: template<class ItemType>
52: ItemType ArrayStack<ItemType>::peek() const
53: {
54: assert(!isEmpty()); // Enforce precondition
55:
56: // Stack is not empty; return top
57: return items[top];
58: } // end peek
59: // End of implementation file.