C++ Template Stack
// //***************************************************************************** //* C++ stack using templates. Note that everything is here; there is no //* implementation CC file. //***************************************************************************** // #ifndef _STACKT_H_ #define _STACKT_H_ // This is a template class. The name T stands for any type. By saying // Stack<whatever>, we create a stack of whatevers. template <class T, int size = 50> class Stack { private: T data[size]; int top_elt; public: // Create an empty stack. Stack() { top_elt = -1; } // Add an element to the stack. Note that you must send a pointer, // and the stack actually stores the pointer. void push(T e) { data[++top_elt] = e; } // Pop the stack and return the top item pointer. If the stack is // empty, return the null pointer. T pop() { if(empty()) return 0; else return data[top_elt--]; } // Return the top item pointer. If none, return the null pointer. T top() { if(empty()) return 0; return data[top_elt]; } // Tell if the stack is empty. bool empty() { return top_elt == -1; } }; #endif