Chain-Stack Simple template code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.