# Include <iostream. h> template <class T> struct Stu {t data; Stu <t> * Next ;}; template <class T> class linkstack {public: linkstack () {Top = NULL ;}~ Linkstack (); void push (t x); t POP (); t gettop () {If (top! = NULL) return top-> data ;}; int empty () {Top = NULL? Return 1: Return 0 ;}; PRIVATE: Stu <t> * Top ;}; template <class T> class linkstack <t >:: push (t x) {S = new Stu; s-> DATA = x; s-> next = top; Top = s;} template <class T> class linkstack <t>: Pop () {Stu <t> * P; t x; If (Top = NULL) throw "wrong"; X = Top-> data; P = top; Top = Top-> next; delete P; return X;} int main () {linkstack <int> S1; int m; do {cout <"-----------------------" <Endl; cout <"input 1 starts to stack" <Endl; cout <"input 2 starts to stack" <Endl; cout <"input 3 starts to read the top of the stack" <Endl; cout <"input 4 starts to judge whether it is null" <Endl; CIN> m; Switch (m) {Case 1: cout <"Enter the element value of the stack" <Endl; CIN> A; s1.push (a); break; Case 2: cout <"this operation is an outbound stack" <Endl; s1.pop (); break; Case 3: int V; cout <"this operation is the top of the read stack" <Endl; s1.gettop (); break; Case 4: cout <"this operation is to judge that the stack is empty" <Endl; s1.empty (); break;} while (1); Return 0 ;}
Implementation of the "chain stack" code [Iot 1132-11]