9. 实现:一维数组 s[M]
栈满 栈空
top
5 top F 5 5
4 top E 4 4
3 top D 3 3
top 2
2 top C 2
top B 1
1 top B 1
top top A 0
base 0 top A 0 base
base 出栈
空栈 进栈
栈底指针 b a s e , 始 设数组维数为 M
终指向栈底位置; to p =b a s e , 栈空,此时出栈,
栈顶指针 to p , 其初 则下溢( und e rflo w)
值指向栈底,始终
在栈顶元素的下一
to p =M, 栈满,此时入栈,则
个位置 上溢( o ve rflo w)
10. Status InitStack (SqStack &S)
{// 构造一个空栈 S
S.base=(SElemType*)malloc(STACK_INIT_SIZ
E *sizeof(SElemType));
if (!S.base) exit (OVERFLOW); // 存储分配失败
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
12. Status Pop (SqStack &S, SElemType &e) {
// 若栈不空,则删除 S 的栈顶元素,
// 用 e 返回其值,并返回 OK ;
// 否则返回 ERROR
if (S.top == S.base) return ERROR;
e = *--S.top;
return OK;
}
44. 空
队 ^
front rear
x 入队 x ^ Q.rear -> next=p
front rear Q.rear=p
y 入队 x y ^
front rear
x 出队 x y ^
front rear
y 出队 ^ p= Q.front -> next
Q.front -> next = p -> next
front ear
r