正在加载图片...
第三章栈与队列 3.15 typedef struct( Ele 2] Elemtype *top[2]: BDStacktype;/双向栈类型 Status Init Stack (BDStacktype&tws,intm)∥/初始化一个大小为m的双向栈tws tws base[0](Elemtype*)malloc(sizeof(Elemtype) tws base[l=tws base[0]+m tws top[O]tws base[o] tws. top[lF=tws base[1] return OK i //nit Stack Status push( BDStacktype& wS, int i, Elemtype x入栈=0表示低端栈=1表示高 端栈 if(tws. top[ oPts. top[ D return OVERFLOW,∥/注意此时的栈满条件 if(i-0)*tws. top[0J++= Ise if(F=1)*tws. top[1]-- else return erROR: turn oK i//push Status pop( BDStacktype& tws, int 1, Elemtype&x/x出栈=0表示低端栈r=1表示 高端栈 if(i==0) if(tws top[O==tws base[od return OVERFLOw =*--tws top[O]: else if(F==1) f(tws top[1ltws base[ld return OVERFLOw x=*++tws. top[1]; else return erROR. eturn oK g//pop第三章 栈与队列 3.15 typedef struct{ Elemtype *base[2]; Elemtype *top[2]; }BDStacktype; //双向栈类型 Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为 m 的双向栈 tws { tws.base[0]=(Elemtype*)malloc(sizeof(Elemtype)); tws.base[1]=tws.base[0]+m; tws.top[0]=tws.base[0]; tws.top[1]=tws.base[1]; return OK; }//Init_Stack Status push(BDStacktype &tws,int i,Elemtype x)//x 入栈,i=0 表示低端栈,i=1 表示高 端栈 { if(tws.top[0]>tws.top[1]) return OVERFLOW; //注意此时的栈满条件 if(i==0) *tws.top[0]++=x; else if(i==1) *tws.top[1]--=x; else return ERROR; return OK; }//push Status pop(BDStacktype &tws,int i,Elemtype &x)//x 出栈,i=0 表示低端栈,i=1 表示 高端栈 { if(i==0) { if(tws.top[0]==tws.base[0]) return OVERFLOW; x=*--tws.top[0]; } else if(i==1) { if(tws.top[1]==tws.base[1]) return OVERFLOW; x=*++tws.top[1]; } else return ERROR; return OK; }//pop
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有