PHP实现一个类,实现一个数组的循环队列,入队列、出队列

class list {
    private $size = 100;
    private $arr = [];

    public function __construct(int $size)
    {
        if ($size > 1) {
            $this->size = $size;
        }
    }

    //入
    public function push(string $data)
    {
        if (count($this->arr) == $this->size) {
            return false;
        } else {
            $this->arr[] = $data;
            return true;
        }
    }

    //出
    public function pull()
    {
        if (empty($this->arr)) {
            return false;
        } else {
            return array_shift($this->arr);
        }
    }

    //是否为空
    public function isEmpty()
    {
        return empty($this->arr);
    }

    //长度
    public function getSize()
    {
        return count($this->arr);
    }
}
发布了115 篇原创文章 · 获赞 101 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/Alen_xiaoxin/article/details/105235196
今日推荐