1#include <stdio.h>2#include <malloc.h>3#include <stdlib.h>4#include <math.h>5 6 #defineStack_init_size 100//å˜å ' ¨ç©ºé-´åˆ?始分é ...? ɇ?7 #defineStackincrement 10//å˜å ' ¨ç©ºé-´åˆ†é ...? Å¢?é‡?8 #defineSelemtype Char//Ž "ʼn?æ °æ?®ç±»å?‹9 Tentypedefstruct One { ASelemtype *Base;//æˆåº 朇é ' ˆï¼› -Selemtype *top;//ƈ顶朇é ' Ɋ - intStackSize//Ž "ʼn?å ²åˆ†é ...? Çš "Řå ' ¨ç©ºé-´ 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定义äº?math.h文件ä¸ï¼œå...¶å€¼ä¸º3<exitå‡½æ °å®šä¹‰åœ¨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定义äº?math.h文件ä¸ï¼œå...¶å€¼ä¸º3<exitå‡½æ °å®šä¹‰åœ¨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 intClearstack (sqstack S) - { theS.top =NULL; theS.Base=NULL; theS.Base= (selemtype*)malloc(Stack_init_size *sizeof(Selemtype));94 if(! S.Base) the { theprintf"CREATE error!\n"); theExit (OVERFLOW);//overflow定义äº?math.h文件ä¸ï¼œå...¶å€¼ä¸º3<exitå‡½æ °å®šä¹‰åœ¨stdlib.hä¸98 } AboutS.top = S.Base; -S.stacksize =stack_init_size;101 return 1;102 }103 104 voidStacktraverse (sqstack S) the {106Selemtype *p = S.Base;107 while(P! =s.top)108 {109printf"%c", * (p++)); the }111printf"\ n"); the }113 the intMain () the { theSqstack S =Initstack ();117 CharC;118printf"çëêäè룬windowsïµí³ïâçëòôctrl+z½áêøêäèë\n");119c =GetChar (); - while(c! =EOF)121 {122 if(c! =EOF)123 {124 Switch(c) the {126 Case '#': s = Pop (s); Break;127 Case '@': Clearstack (S); Break; - default: S =Push (s,c);129 } the }131c =GetChar (); the }133 Stacktraverse (S);134 135}
Application of data structure stack--line editing program