欢迎来到天天文库
浏览记录
ID:10639774
大小:34.50 KB
页数:15页
时间:2018-07-07
《数据结构c语言版_栈实现表达式求值(c language version of the data structure _ expression evaluation stack)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、数据结构c语言版_栈实现表达式求值(Clanguageversionofthedatastructure_expressionevaluationstack)*ClanguageversionofthedatastructurestacktoachieveexpressionevaluationP52-54Compilerenvironment:Dev-C++4.9.9.2Date:February10,2011*/TypedefintSElemType;//stackelementtype#defineSTACK_INIT_SIZE//10initialstoragespaceall
2、ocation#defineSTACKINCREMENT//2incrementalstoragespaceallocationTheorderofthestackstorage//P46TypedefstructSqStack{SElemType*base;//inthestackstructurebeforeandafterthedestruction,thevalueofbaseisNULLSElemType*top;//stackpointerIntstacksize;//storagespacecurrentlyallocatedtounits,elements}SqStac
3、k;//theorderofthestack//constructsanemptystackS.IntInitStack(SqStack*S){//assignaspecifiedsizeofthestoragespaceforthebottomofthestack(*S).Base=(SElemType*malloc)(STACK_INIT_SIZE*sizeof(SElemType));If((*S).Base!)Exit(0);//memoryallocationfailure(*S).Top=.Base(*S);//stackbottomandstackthesamerepre
4、sentationofanemptystack(*S).Stacksize=STACK_INIT_SIZE;Return1;}//ifthestackisnotempty,thenuseeStoreturnthetopofthestack,andreturns1;otherwise0.IntGetTop(SqStackS,SElemType*e){If(S.top>S.base){*e=*(S.top-1);//stackpointertothetopofthenextpositiontothetopofthestackReturn1;}ElseReturn0;}//insertthe
5、elementsofEasthenewtopelement.IntPush(SqStack*S,SElemTypeE){If((*S).Top-(*S).Base(*S)>=.Stacksize)//stackfulladditionalstoragespace.{(*S).Base=(SElemType*)realloc((*S).Base,((*S).Stacksize+STACKINCREMENT)*sizeof(SElemType));If((*S).Base!)Exit(0);//memoryallocationfailure(*S)=.Top(*S).Base+(*S).S
6、tacksize;(*S).Stacksize=STACKINCREMENT;}*((*S).Top)++=e;Thisequation++*//thesamepriority,buttheiroperationmodeisfromrighttoleftReturn1;}//ifthestackisnotempty,thendeletetheSstackelements,withEvaluereturn,andreturn1;otherwise0.IntPop(SqStack*S,SElemType*e){If((*S)=.Top(*S).Base)Return0;*e:=(*S).T
7、op;Thisequation++*//thesamepriority,buttheiroperationmodeisfromrighttoleftReturn1;}Accordingtothetextbook/Table3.1,determinethepriorityrelationofthetwosymbolsSElemTypePrecede(SElemTypeT1,SElemTypeT2){SElemTypef;Switch(T2){Th
此文档下载收益归作者所有