Review using C language implementation stack
PS: There is a dynamic way to increase the memory, pay attention to
1 #defineStack_init_size 1002 #defineStackincrement 1003 4typedefstruct{5Selemtype *Base;6Selemtype *top;7 }sqstack;8 9Status Initstact (sqstack%R) {TenS.Base= (Selemtype *)malloc(Stack_init_size *sizeof(Selemtype)); One if(! S.Base) A exit (OVERFLOW); -S.top = S.Base; -S.stacksize =stack_init_size; the returnOK; - } - -Status GetTop (Sqstack s,selemtype &e) {//The top element is saved in e + if(S.top = = S.)Base) - returnERROR; +E = * (s.top-1); A returnOK; at } - -Status Push (Sqstack &S,selemtype e) { - if(S.top-s.Base>= s.stacksize) {//Stack is full - //Dynamically increase memory -S.Base= (Selemtype *)realloc(S.Base, (s.stacksize+stackincrement) *sizeof(Selemtype)); in if(! S.Base) - exit (OVERFLOW); toS.top = S.Base+s.stacksize; +S.stacksize + =stackincrement; - } the*s.top++ =e; * returnOK; $ }Panax Notoginseng -Status Pop (Sqstack &s,selemtype &e) { the if(S.top = = S.)Base) + returnERROR; AE = *--S.top; the returnOk; +}
Implementation of the Stack