(3)求栈的长度StackLength(s)返回栈s中的元素个数,即栈指针加1的结果*s)int StackLength(SqStackreturn(s->top+1);16
16 (3)求栈的长度 StackLength(s) 返回栈s中的元素个数,即栈指针加1的结果。 int StackLength(SqStack *s) { return(s->top+1); }
(4)判断栈是否为空StackEmpty(s)栈S为空的条件是s->top==-1。int StackEmpty(SqStack *s)return(s->top==-1):17
17 (4)判断栈是否为空 StackEmpty(s) 栈S为空的条件是s->top==-1。 int StackEmpty(SqStack *s) { return(s->top==-1); }
(5)入栈Push(&s , e)在栈不满的条件下,先将栈指针增1,然后在该位置上插入元素e。intPush(SqStack *&s, ElemType e)if (s->top==MaxSize-1)return 0:/*栈满的情况,即栈上溢出*s->top++;s->elem[s->top]=e;return 1;18
18 (5)入栈 Push(&s,e) 在栈不满的条件下,先将栈指针增1,然后在该位 置上插入元素e。 int Push(SqStack *&s,ElemType e) { if (s->top==MaxSize-1) return 0; /*栈满的情况,即栈上溢出*/ s->top++; s->elem[s->top]=e; return 1; }
(6)出栈Pop(&s,&e)在栈不为空的条件下,先将栈顶元素赋给e,然后将栈指针减1。int Pop(SqStack *&s , ElemType &e)if (s->top==-1) return 0;/*栈为空的情况,即栈下溢出*e=s->elem[s->top];s->top--;return 1;19
19 (6)出栈 Pop(&s,&e) 在栈不为空的条件下,先将栈顶元素赋给e,然 后将栈指针减1。 int Pop(SqStack *&s,ElemType &e) { if (s->top==-1) return 0; /*栈为空的情况,即栈下溢出*/ e=s->elem[s->top]; s->top-; return 1; }
(7)取栈顶元素GetTop(s)在栈不为空的条件下,将栈顶元素赋给e。int GetTop(SqStack *s : ElemType &e)if (s->top==-1)return 0:/*栈为空的情况,即栈下溢出*/e=s->elem[s->top];return 1;20
20 (7)取栈顶元素 GetTop(s) 在栈不为空的条件下,将栈顶元素赋给e。 int GetTop(SqStack *s,ElemType &e) { if (s->top==-1) return 0; /*栈为空的情况,即栈下溢出*/ e=s->elem[s->top]; return 1; }