php系列之通过PHP数组实现队列

php系列之通过PHP数组实现队列

废话少说,上代码(talk is easy ,show me the code):

	_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

你可能感兴趣的:(php)