/* * This class implements a stack by directly using a linked list. */ #ifndef _lnkstack_h_ #define _lnkstack_h_ template class Stack { public: // Initialize the stack empty. Stack() { m_head = nullptr; m_size = 0; } // Push the argument item. void push(const T &itm) { m_head = new node(itm, m_head); ++m_size; } // Pop the argument item, and return true, or return false if the // the stack is empty. bool pop(T &itm) { if(m_head == nullptr) return false; itm = m_head->data(); node *zombie = m_head; m_head = m_head->next(); delete zombie; --m_size; return true; } // Convenience to pop and return the top object. Only works // for types where T() is a valid expression. T pop() { T ret; if(pop(ret)) return ret; return T(); } // Tell if empty. bool empty() { return m_head == nullptr; } int size() { return m_size; } private: // Nodes for the linked list. class node { public: node(const T &d, node *n = nullptr) { m_data = d; m_next = n; } const T& data() { return m_data; } node *next() { return m_next; } private: T m_data; node *m_next; }; node * m_head; // Head of the linked list. int m_size; // Size of the stack. }; #endif