PHP代码实现
class Listo
{
public function getlist()
{
return $this->arr;
}
private $arr = array();
private $len = 0;
private $head = 0;
private $end = 0;
public function init($len){
$this->len = $len;
// 最后一个位置做区分用
for ($i=0; $i <= $len; $i++) {
$this->arr[$i] = null;
}
}
public function push($d){
$cur = $this->head;
if (++$this->head > $this->len ) {
$this->head = 0;
}
if ($this->head === $this->end) {
$this->head = $cur;
return null;
}
$this->arr[$cur] = $d;
}
public function pop()
{
if ($this->end === $this->head) {
return null;
}
$cur = $this->end;
if (++$this->end > $this->len ) {
$this->end = 0;
}
return $this->arr[$cur];
}
}
有疑问加站长微信联系(非本文作者))