php系列----->通过PHP数组实现队列

简介: 废话少说,上代码(talk is easy ,show me the code):<?php/** * 通过 PHP 数组实现的队列 */class SimpleQueue{ private $_queue = []; private $_size = 0; ...
废话少说,上代码(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
相关文章
|
7月前
|
PHP
php数组随机排序
PHP中对数据进行随机排序
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
|
3月前
|
JSON PHP 数据格式
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
php 删掉空的数组 json数据. 空数据(false 0 ““ null)
|
9月前
|
存储 PHP
php使用数组存储用户数据进行登录的封装函数
php使用数组存储用户数据进行登录的封装函数
46 0
|
9月前
|
PHP
php模版引擎smarty中判断数组是否为空自动输输出
php模版引擎smarty中判断数组是否为空自动输输出
62 0
|
4月前
|
JSON PHP 数据格式
【PHP学习】—数组的定义和遍历(三)
【PHP学习】—数组的定义和遍历(三)
|
7月前
|
搜索推荐 算法 PHP
PHP 数组(Array) - 排序算法
PHP 数组(Array) - 排序算法
23 0
|
7月前
|
PHP 索引
PHP 数组(Array)
PHP 数组(Array)
30 0