正在加载图片...
int d } SElemType;//栈中元素类型 ypedef struct I * base int stacksize } Stack;//栈类型 Status InitStack(Stack &S)i /构造空栈S S base=(SElemType*)malloc(STACK_ INIT SIZE*sizeof(SElemType)); if(!S.base)exit( VERFLOW);//存储分配失败 S.top= S base;/空栈 S. stacksize=STACK INIT SIZE: return 1/InitStack Status Push(Stack &S, SElemType e)i //插入e为栈顶元素 f(S.top-S.base>=S. stacksize)//栈满则应重新分配空间 S base=(SElemType *)realloc(S base, (S stacksize+STACKINCREMENT)*sizeof (SElemType)): if(!S base) exit(OVERFLOW S.top=(S.base+S. stacksize);//使得S.top重新指向原栈顶,因 realloc S. stacksize+=STACK INIT Size *S.top+=e;∥/top指向待插入位置 1//Push Status Pop (Stack &S, SElemType &e)f //若栈不空则栈顶元素出栈并用e带回其值 if(S top==S base)return ERROR //栈顶元素为*(S.top-1) return OK. Status GetTop(Stack S, SElemType &e)I if(S top==S base)return ERROR *(S.top-1);//注意top指向待插入位置 return OK Status StackEmpty( Stack s){/判空int di; }SElemType;//栈中元素类型 typedef struct{ SElemType *base; SElemType *top; int stacksize; }Stack;//栈类型 Status InitStack(Stack &S){ //构造空栈S S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); //存储分配失败 S.top=S.base; //空栈 S.stacksize=STACK_INIT_SIZE; return OK; }//InitStack Status Push(Stack &S, SElemType e){ //插入e为栈顶元素 if(S.top-S.base>=S.stacksize) //栈满则应重新分配空间 { S.base=(SElemType *) realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top=(S.base+S.stacksize);//使得S.top重新指向原栈顶,因realloc S.stacksize+=STACK_INIT_SIZE; } *S.top++=e; //top指向待插入位置 return OK; }//Push Status Pop(Stack &S,SElemType &e){ //若栈不空则栈顶元素出栈并用e带回其值 if(S.top==S.base)return ERROR; else e=*(--S.top); //栈顶元素为*(S.top-1) return OK; } Status GetTop(Stack S,SElemType &e){ if(S.top==S.base)return ERROR; e=*(S.top-1); //注意top指向待插入位置 return OK; } Status StackEmpty(Stack S){//判空
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有