把货架看作长度为 n 的数组,左指针 hp 表示下一个从左侧存入的位置(从 0 向右移动),右指针 sp 表示下一个从右侧存入的位置(从 n-1 向左移动):
hp = 0,sp = n-1,货架为给定初值。hp > sp ⇒ 无可用空间,Memory allocation failure;否则写 a[hp]=x,hp++。hp == 0 ⇒ 左侧无货,Invalid heap address;否则 hp--(不清空货物值)。hp > sp ⇒ 无可用空间,Memory allocation failure;否则写 a[sp]=x,sp--。sp == n-1 ⇒ 右侧无货,Invalid stack address;否则 sp++(不清空货物值)。小 B 有一个长度为 n 的线性货架,货架上的格子从左到右编号为 0 到 n−1。每个格子可以存放一个货物。初始时,第 i 个格子存有一个非负整数值 ai。
小 B 使用两个指针管理货物的存入和取出:左指针 L 指向下一个从左侧存入货物时应放置的格子(初始为 0),右指针 R 指向下一个从右侧存入货物时应放置的格子(初始为 n−1)。
Scan the QR code below with WeChat to sign in
First-time scan will create your account automatically
请使用微信扫描下方二维码完成注册