第三章 栈与队列 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(m*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 3