废话少说,上代码(talk is easy ,show me the code):
<?php
/**
* 通过 PHP 数组实现的队列
*/
class SimpleQueue
{
private $_queue = [];
private $_size = 0;
public function __construct($size = 10)
{
$this->_size = $size;
}
// 入队
public function enqueue($value)
{
if (count($this->_queue) > $this->_size) {
return false;
}
array_push($this->_queue, $value);
}
// 出队
public function dequeue()
{
if (count($this->_queue) == 0) {
return false;
}
return array_shift($this->_queue);
}
public function size()
{
return count($this->_queue);
}
}
$queue = new SimpleQueue(5);
$queue->enqueue(1);
$queue->enqueue(3);
$queue->enqueue(5);
var_dump($queue->dequeue()); # 1
var_dump($queue->size()); # 2