环形队列PHP,环形队列 - Go语言中文网 - Golang中文社区
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];
}
}
有疑問加站長微信聯系(非本文作者))
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的环形队列PHP,环形队列 - Go语言中文网 - Golang中文社区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab中的rem和mod,matl
- 下一篇: php curl ob start,cu