Reproduced Getting started with the C + + stack

Source: Internet
Author: User

Disclaimer: Transfer from http://www.cnblogs.com/pengshao/archive/2011/12/26/2301461.html

Header file StackDemo.h

1 #pragmaOnce//commonly used C + + pragma, the header file is the first to join, to ensure that the header files are only compiled once2typedefintDataType;3 Const intMaxstatcksize = -;//define the size of the stack4 classStackdemo5 {6  Public:7     //To analyze a constructor function8Stackdemo (void);9~stackdemo (void);Ten  One     //push stack out stack operation A     voidPush (DataType item); -DataType Pop (void); -     voidClearstack (void); the  -     //access the top of the stack, return to the top of the stack current subscript -DataType Peek (void)Const; -  +     //detection of vertebral stack -     BOOLIsEmpty (void)Const; +     BOOLIsfull (void)Const; A  at Private: - DataType stacklist[maxstatcksize]; -     intTos//Top of Stack -};
View Code

Implement StackDemo.cpp

1#include"StackDemo.h"2#include <iostream>3 using namespacestd;4Stackdemo::stackdemo (void)5 {6      This->tos =-1;7 }8Stackdemo::~stackdemo (void)9 {Ten      This->tos =-1; One } A voidStackdemo::P ush (DataType Item) - { -     //whether the stack is full the     if(!isfull ()) -     { -tos++; -          This->stacklist[tos] =item; +  -     } +     Else Acout <<"Out of the stack!"<<Endl; at } -  -DataType Stackdemo::P op (void) - { -     if(!isEmpty ()) -     { in         intEBP =tos; -TOS--; to         returnSTACKLIST[EBP]; +     } -     Else the         return-1; * } $ Panax NotoginsengDataType Stackdemo::P eek (void)Const - { the     returntos; + } A  the voidStackdemo::clearstack () + { -      for(inti = TOS; I >=0; i--) $Stacklist[i] =0; $TOS =-1; -cout <<"Clear Stack done!"<<Endl; - } the  - BOOLStackdemo::isfull (void)ConstWuyi { the     returnTos > Maxstatcksize?true:false; - } Wu  - BOOLStackdemo::isempty (void)Const About { $     returnTOS <0?true:false; -}
View Code

Main.cpp

1#include <iostream>2#include"StackDemo.h"3 using namespacestd;4  5 intMain ()6 {7Stackdemo *SD =NewStackdemo ();8Sd->push (Ten);//Press Stack9Sd->push ( -);//Press StackTenSd->push ( -);//Press Stack Onecout <<"Stack TOP:"<< Sd->peek () <<Endl; A      for(intI=0;i<3; i++) -cout <<"POP:"<< Sd->pop () <<Endl; -  thecout <<"Stack TOP:"<< Sd->peek () <<Endl; -  -     return 0; -   +}
View Code

  

Reproduced Getting started with the C + + stack

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.