1 <?php 2 # 双向队列 3 class Deque { 4 public $queue = array(); 5 6 public function frontAdd($obj) { 7 array_unshift($this->queue, $obj); 8 } 9 10 public function frontRemove() { 11 return array_shift($this->queue); 12 } 13 14 public function rearAdd($obj) { 15 array_push($this->queue, $obj); 16 } 17 18 public function rearRemove() { 19 return array_pop($this->queue); 20 } 21 22 public function size() { 23 return count($queue); 24 } 25 } 26 27 $deque = new Deque(); 28 $deque->rearAdd(2); 29 $deque->rearAdd(2); 30 $deque->frontAdd(1); 31 $deque->frontAdd(1); 32 $deque->rearAdd(2); 33 $deque->rearAdd(2); 34 print_r($deque); 35 $deque->frontRemove(); 36 $deque->rearRemove(); 37 print_r($deque); 38 ?>
Deque Object ( [queue] => Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 2 [4] => 2 [5] => 2 ) ) Deque Object ( [queue] => Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 2 ) )