PHP模拟队列操作,代码如下
<?php
/**
* php队列测试
* @author lee.chuke@gmail.com
* @date 2014-12-02
*/
class Queue{
private $_data = array();
private $_top = null;
private $_end = 0;
public function push($data){
if($this->_top===null){
$this->_top = 0;
}else{
$this->_top++;
}
$this->_data[$this->_top] = $data;
return true;
}
public function pop(){
if(empty($this->_data)){
return false;
}
if($this->_top==0){
return false;
}
if($this->_end>$this->_top){
return false;
}
$ret = $this->_data[$this->_end];
unset($this->_data[$this->_end]);
$this->_end++;
return $ret;
}
public function getData(){
return $this->_data;
}
}
$queue = new Queue();
$queue->push('aa');
$queue->push('bb');
$queue->push('cc');
$queue->push('dd');
echo $queue->pop();
echo $queue->pop();
$all = $queue->getData();
print_r($all);
结果如下:
aabbArray
(
[2] => cc
[3] => dd
)