// Stack in the form of custom linked list # include <iostream> using namespace STD; Template <class T> class node {public: t data; node <t> * link ;}; template <class T> class Consumer stack {public: Consumer stack () {Top = 0 ;}~ Consumer stack (); bool isempty () const {return Top = 0;} bool isfull () const; t top () const; consumer stack <t> & pushstack (const T & X); consumer stack <t> & popstack (T & X); node <t> * Top; void show () const ;}; class outofbounds {public: outofbounds () {cout <"out of bounds! "<Endl ;}; // exception class nomem {public: nomem () {cout <" no memory! "<Endl ;}}; // cause nomem exception caused by new instead of xalloc. // if you want to restore the original behavior, you can call the following: // _ set_new_handler (old_handler ); int my_new_handler (size_t size) {Throw nomem ();} template <class T> void consumer stack <t >:: show () const {node <t> * Current = top; while (current) {cout <Current-> data <Endl; current = Current-> link ;}} // The stack destructor template <class T> producer stack <t> ::~ Consumer stack () {node <t> * Next; while (top) {next = Top-> link; Delete top; Top = next ;}} // determine whether the current stack has a fully loaded template <class T> bool stack <t>: isfull () const {try {node <t> * P = new node <t>; delete P; return false;} catch (nomem) {return true ;}// return the Header element data template of the current stack <class T> T linkedstack <t> :: top () const {If (isempty () Throw outofbounds (); Return top-> data ;} // press the data into the stack template <class T> consumer stack <t> & Consumer stack <t>: pushstack (const T & X) {node <t> * P = new node <t>; P-> DATA = x; P-> link = top; Top = P; return * This ;} // Delete the top element of the stack and send it to the xtemplate <class T> consumer stack <t> & Consumer stack <t>: popstack (T & X) {If (isempty ()) throw outofbounds (); X = Top-> data; node <t> * P = top; Top = Top-> link; Delete P; return * This;} int main () {consumer stack <int> mystack; mystack. pushstack (1); mystack. pushstack (2); mystack. pushstack (3); mystack. pushstack (4); mystack. pushstack (5); mystack. pushstack (6); mystack. show (); int popnode; mystack. popstack (popnode); cout <"pop one from Stack:" <popnode <Endl; mystack. show (); Return 0 ;}