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