Application of data structure stack--test of bracket matching

Source: Internet
Author: User

1#include <stdio.h>2#include <malloc.h>3#include <stdlib.h>4#include <math.h>5 6 #defineStack_init_size 100//Initial amount of storage space allocated7 #defineStackincrement 10//storage space allocation increment8 #defineSelemtype Char//Current Data type9 Tentypedefstruct One { ASelemtype *Base;//stack bottom pointer; -Selemtype *top;//stack Top pointer -     intStackSize//the currently allocated storage space the }sqstack; -  - sqstack initstack () - { + Sqstack S; -S.Base= (Selemtype *)malloc(Stack_init_size *sizeof(Selemtype)); +     if(! S.Base) A     { atprintf"CREATE error!\n"); -Exit (OVERFLOW);//overflow is defined in the Math.h file with a value of 3,exit function defined in Stdlib.h -     } -S.top = S.Base; -S.stacksize =stack_init_size; -     returnS; in } -  to intstackempty (sqstack S) + { -     if(S.top = = S.)Base) the     { *         return 1; $     }Panax Notoginseng     Else -     { the         return 0; +     } A } the  + selemtype GetTop (sqstack S) - { $     if(S.top = = S.)Base) $     { -         return 0; -     } the     Else -     {Wuyi         return* (S.top-1); the     } - } Wu  - sqstack Push (sqstack S, Selemtype e) About { $     if(S.top-s.Base>=s.stacksize) -     { -S.Base= (selemtype*)realloc(S.Base, (s.stacksize + stackincrement) *sizeof(Selemtype)); -         if(! S.Base) A         { +printf"error\n"); theExit (OVERFLOW);//overflow is defined in the Math.h file with a value of 3,exit function defined in Stdlib.h -         } $S.top = S.Base+s.stacksize; theS.stacksize + =stackincrement; the     } the     Else the     { -* (s.top++) =e; in     } the         returnS; the } About  the sqstack Pop (sqstack S) the { the     if(S.top = = S.)Base) +     { -printf"null\n"); the         returnS;Bayi     } the     Else the     { -s.top--; -         returnS; the     } the } the  the intMain () - { theSqstack S =Initstack (); the     CharC; the      while(1)94     { thescanf"%c",&c); the         if(c = ='['|| c = =']'|| c = ='('|| c = =')'|| c = ='{'|| c = ='}') the         {98             if(c = =']') About             { -                 if(GetTop (S) = ='[')101                 {102S =Pop (S);103                 }104             } the             Else if(c = =')')106             {107                 if(GetTop (S) = ='(')108                 {109S =Pop (S); the                 }111             } the             Else if(c = ='}')113             { the                 if(GetTop (S) = ='{') the                 { theS =Pop (S);117                 }118             }119             Else -             {121S =Push (s,c);122             }123         }124         Else the         {126              Break;127         } -     }129      the     if(Stackempty (S))131     { theprintf"yes!\n");133     }134     Else135     {136printf"no!\n");137     }138 139}

Application of data structure stack--test of bracket matching

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.