php實現隊列


<?php
//雙向隊列的實現
class DoubleEndedQueue{
public $elements;
public function __construct(){//析構函數,創建一個數組
$this->elements = array();
}
public function push($element){//array_unshift() 函數在數組開頭插入一個或多個元素。
array_unshift($this->elements , $element);
}
public function pop(){
return array_shift($this->elements);//PHP array_shift() 函數刪除數組中的第一個元素_
}
public function inject($element){//給數組末尾追加元素,無指定下標,默認為數字
$this->elements[] = $element;
}
public function eject(){
array_pop($this->elements);//PHP array_pop() 函數刪除數組中的最后一個元素
}
}

//實例化該類,測試下
$a=new DoubleEndedQueue();

$a->inject(‘aa’);//給數組末尾追加元素,無指定下標,默認為數字
$a->inject(‘dd’);
$a->inject(‘cc’);
$a->inject(‘dd’);
$a->push(’111′);//函數在數組開頭插入一個或多個元素。
$a->pop();//PHP array_shift() 函數刪除數組中的第一個元素_
$a->eject();//PHP array_pop() 函數刪除數組中的最后一個元素
print_r($a->elements);
?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM