思路概述
把货架看作长度为 n 的数组,左指针 hp 表示下一个从左侧存入的位置(从 0 向右移动),右指针 sp 表示下一个从右侧存入的位置(从 n-1 向左移动):
- 初始:
hp = 0,sp = n-1,货架为给定初值。
- 左侧存入(opt=0,给定 x):若
hp > sp ⇒ 无可用空间,Memory allocation failure;否则写 a[hp]=x,hp++。
- 左侧取出(opt=1):若
hp == 0 ⇒ 左侧无货,Invalid heap address;否则 hp--(不清空货物值)。
- 右侧存入(opt=2,给定 x):若
hp > sp ⇒ 无可用空间,Memory allocation failure;否则写 a[sp]=x,sp--。
- 右侧取出(opt=3):若
sp == n-1 ⇒ 右侧无货,Invalid stack address;否则 sp++(不清空货物值)。