(3)判断栈是否为空 StackEmpty(s) 栈S为空的条件是s>tp=-1 bool stackEmpty(sqStack *s) return(s->top==-1) 16
16 (3) 判断栈是否为空StackEmpty(s) 栈S为空的条件是s->top==-1。 bool StackEmpty(SqStack *s) { return(s->top==-1); }
(4)进栈Push(,e) 在栈不满的条件下,先将栈顶指针增1,然 后在该位置上插入元素e bool Push(sqstack*&s, ElemType e) i if(s->top==MaxSize-1)return false; 栈满的情况,即栈向上溢出 s-top++ s→> data s->topl=e; return true
17 (4) 进栈Push(s,e) 在栈不满的条件下,先将栈顶指针增1,然 后在该位置上插入元素e。 bool Push(SqStack *&s,ElemType e) { if (s->top==MaxSize-1) return false; /*栈满的情况,即栈向上溢出*/ s->top++; s->data[s->top]=e; return true; }
(5)出栈Pop(s,e) 在栈不为空的条件下,先将栈顶元素赋给 ,然后将栈顶指针减1 bool pop(sqstack*&s, Elem Type &e) if(s->top- ==-1)return false; 栈为空的情况,即栈向下溢出* e=s->datas->top; S→>top-; return true } 18
18 (5) 出栈Pop(s,e) 在栈不为空的条件下,先将栈顶元素赋给 e,然后将栈顶指针减1。 bool Pop(SqStack *&s,ElemType &e) { if (s->top==-1) return false; /*栈为空的情况,即栈向下溢出*/ e=s->data[s->top]; s->top--; return true; }
(6)取栈顶元素 GetTop(s,e) 在栈不为空的条件下,将栈顶元素赋给e。 bool getTop(sqstack *S, ElemType &e) if(s->top==-1) return false; 栈为空的情况,即栈向下溢出* e=s->data s->top; return trues 19
19 (6) 取栈顶元素GetTop(s,e) 在栈不为空的条件下,将栈顶元素赋给e。 bool GetTop(SqStack *s,ElemType &e) { if (s->top==-1) return false; /*栈为空的情况,即栈向下溢出*/ e=s->data[s->top]; return true; }
(7)显示栈中元素 DispStack(s) 从栈顶到栈底顺序显示栈中所有元素 void DispStack(sqstack*S int i: for (i=s->top; i>=0; i p rin itf("%c s->data) p rIn tf(n); 20
20 (7) 显示栈中元素DispStack(s) 从栈顶到栈底顺序显示栈中所有元素。 void DispStack(SqStack *s) { int i; for (i=s->top;i>=0;i--) printf("%c ",s->data[i]); printf("\n"); }