Application of stacks (C + + implementation)

Source: Internet
Author: User

The use of C + + language to achieve the creation of stacks, stack, stack, judge the stack empty functions.

1#include <iostream>2#include <string.h>3 using namespacestd;4 5 #defineERROR-16 #defineCORRECT 17 8 //the definition of a stack, which defines a stack structure that contains the stack base pointer base and the top of the stack pointer top9 #definemaxStackSize 100TentypedefstructSqstack One { A     Char Base[maxstacksize]; -     Char*top; - } sqstack; the  - Sqstack Mbstack; -  - //initializes the stack if the stack does not exist (base = = NULL), returns error, points top to base + intInitstack (Sqstack &S) - { +     if(S.Base==NULL) A     { at         return(ERROR); -     } -     Else -     { -S.top = S.Base;//Initialize Stack (empty) -         return(CORRECT); in     } - } to  + //into the stack, if top out of range, return error, the data (e) inserted at the top of the stack at the designated position (top), the top of the stack pointer (top) plus a - intPush (Sqstack &s,Chare) the { *     if(S.top-s.Base) >=maxstacksize) $     {Panax Notoginseng         return(ERROR); -     } the     Else +     { AS.Base[S.top-s.Base] =e; thes.top++ ; +         return(CORRECT); -     } $ } $  - //out of the stack, if it is empty, return error, otherwise take out a data into the variable E, top-- - intPop (Sqstack &s,Char&e) the { -     if(S.top <= S.Base)Wuyi     { the         return(ERROR); -     } Wu     Else -     { AboutE = S.Base[S.top-s.Base-1] ; $s.top-- ; -         return(CORRECT); -     } - } A  + //Judging Stack empty the intStackempty (Sqstack &S) - { $     if(S.top <= S.Base) the     { the         return(ERROR); the     } the     Else -     { in         return(CORRECT); the     } the } About  the //Bracket Matching the intMatchbracket (Sqstack &s,Char*bracketstring) the { +     inti; -     CharC, SC; the BayiInitstack (S);//Empty Stack the      for(i =0; I < strlen (bracketstring); i++) the     { -EBracketstring[i]; -  the         if(C = ='(') || (C = ='[') || (C = ='{')) the         { the Push (S, C); the         } -         if(C = =')') || (C = =']') || (C = ='}')) the         { the             if(Stackempty (S) = =ERROR) the             {94                 return(-2) ;//right more than left the             } the Pop (S, SC); the             if(C = =')') && (SC! ='('))98             { About                 return(-1);//right and left order . -             }101             if(C = =']') && (SC! ='['))102             {103                 return(-1);//right and left order .104             } the             if(C = ='}') && (SC! ='{'))106             {107                 return(-1);//right and left order .108             }                        109         } the     }111  the     if(Stackempty (S)! = ERROR)//Non-empty113     { the         return(-3) ;//more left than right the     } the 117     Else118     {119         return(0) ;//stack empty is correct -     }121 }122 123 //Main function124 intMain (intARGC,Char*argv[]) the {126     intI, samplenum;127     CharBracketstring[maxstacksize]; - 129CIN >>Samplenum; the      for(i =0; i < Samplenum; i + +)131     { theCIN >>bracketstring;133cout << matchbracket (Mbstack, bracketstring) <<Endl;134     }135     return 0 ;136}

Application of stacks (C + + implementation)

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.