| 83 | // Pop an object off the stack |
| 84 | template <typename T> |
| 85 | T Stack<T>::pop() |
| 86 | { |
| 87 | if (isEmpty()) // If it's empty pop() is not valid so throw exception |
| 88 | throw std::logic_error {"Stack empty"}; |
| 89 | |
| 90 | auto* next {m_head->m_next}; // Save pointer to the next node |
| 91 | T item {m_head->m_item}; // Save the T value to return later |
| 92 | delete m_head; // Delete the current head |
| 93 | m_head = next; // Make head point to the next node |
| 94 | return item; // Return the top object |
| 95 | } |
| 96 | |
| 97 | template <typename T> |
| 98 | bool Stack<T>::isEmpty() const { return m_head == nullptr; } |