The following code is only used by me to review the data structure. The practicality of N is low ~~ Haha:>
/// C ++ template technology implements simple chain stack. // # include <cstdlib> # include <iostream> # include <iomanip> # include <stdexcept> // pre-declaration of the chained stack template. template <typename T> class nodes stack; // The Node class template of the chain stack. // template <typename T> class node {friend class each stack <t>; private: T _ value; node <t> * _ pnext; public: node (void ): _ pnext (null) {NULL;} node (const T & Val): _ value (VAL), _ pnext (null) {null ;}}; //// chain stack template. // template <typename T> linoleic SS consumer stack {PRIVATE: node <t> * _ pTop; public: Consumer stack (void );~ Consumer stack (void); bool isempty (void) const; t gettop (void) const; void push (const T & Val); t POP (void );}; template <typename T> inline custom stack <t >:: custom stack (void): _ pTop (null) {NULL;} template <typename T> Custom stack <t> ::~ Consumer stack (void) {node <t> * pdel = NULL; while (null! = _ PTop) {pdel = _ pTop; _ pTop = _ pTop-> _ pnext; Delete pdel ;}} template <typename T> inline bool existing stack <t> :: isempty (void) const {return NULL ==_ pTop;} template <typename T> inline t consumer stack <t >:: gettop (void) const {If (isempty ()) {Throw STD: underflow_error ("Stack Overflow is empty. ");} return _ pTop-> _ value;} template <typename T> inline void multicast stack <t>: Push (const T & Val) {node <t> * pnew = new node <t> (VAL); pnew-> _ pnex T = _ pTop; _ pTop = pnew;} template <typename T> inline t consumer stack <t >:: POP (void) {If (isempty () {Throw STD :: underflow_error ("Stack Overflow is empty. ");} t value = _ pTop-> _ value; node <t> * pdel = _ pTop; _ pTop = _ pTop-> _ pnext; Delete pdel; return Value;} // test stack. // int main (void) {const size_t max_size = 20; Consumer stack <int> stack; STD: cout <"stack" <(stack. isempty ()? "": "No") <"is empty. "<STD: Endl; For (size_t I = 0; I <max_size; ++ I) {stack. push (I); STD: cout <STD: SETW (3) <stack. gettop ();} STD: cout <STD: Endl; STD: cout <"stack" <(stack. isempty ()? "": "No") <"is empty. "<STD: Endl; For (size_t I = 0; I <max_size; ++ I) {STD: cout <STD: SETW (3) <stack. pop ();} // STD: cout <STD: SETW (3) <stack. pop (); STD: cout <STD: Endl; STD: cout <"stack" <(stack. isempty ()? "": "No") <"blank." <STD: Endl; return exit_success ;}