1#include <iostream>2#include <stdlib.h>3 using namespacestd;4 5Template <classT>6 classLinkstack;7Template <classT>8 classStacknode9 {Ten Public: OneFriendclassLinkstack<t>; A Private: - T data; -Stacknode<t> *Next; the }; -template<classT> - classLinkstack - { + Public: -Linkstack () {top =NULL;} +~Linkstack (); A BOOLStackempty () at{returntop = =NULL;} - voidPush (T x); - voidPop (T &x); - T GetTop (); - - Private: instacknode<t>*top; - }; totemplate<classT>//into the stack + voidLinkstack<t>::P ush (T x) - { theStacknode<t> *p; *p =NewStacknode<t>; $P->data =x;Panax NotoginsengP->next =top; -top =p; the } +Template <classT>//returns the top element of the stack AT linkstack<t>:: GetTop () the { + if(Stackempty ()) - { $cout <<"is empty"<<Endl; $ return-1; - } - Else returnTop->data; the } -Template <classT>//out of the stackWuyi voidLinkstack<t>::P op (T &x) the { -Stacknode<t> *p =top; Wu if(Stackempty ()) - { Aboutcout <<"is empty"<<Endl; $ } - Else - { -x = p->data; Atop = p->Next; + Deletep; the } - } $Template <classT> theLinkstack<t>::~linkstack ()// Destructors the { theStacknode <T> *s; the while(top) - { ins = top->Next; the Deletetop; thetop =s; About } the } the the voidConversionintNintD//Binary Conversion + { -linkstack<int>S; the inti;Bayi while(N) the { theS.push (N%d); -n = n/D; - the } the while(!s.stackempty ()) the { the S.pop (i); -cout <<i; the the } thecout <<Endl;94 } the voidMain () the { the intN -), D (2);98cout<<"decimal 64 is converted to binary:"; About conversion (N, d); -System"PAUSE");101 return;102}
Chain-Stack Simple template code