栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

环形队列php

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

环形队列php


    public $queue = [];
    public $size;
    public $front = 0; # 队首指针
    public $rear = 0; # 队尾指针

    public function __construct($size = 10)
    {
        $this->size = $size;
        for ($i = 0; $i < $this->size; $i++) {
            $this->queue[] = '';
        }
    }

    # 入列
    public function push($element)
    {
        if (!$this->is_filled()) {
            $this->rear = ($this->rear + 1) % $this->size;
            $this->queue[$this->rear] = $element;
        } else {
            throw new Exception("队列已满");
        }
    }

    # 出列
    public function pop()
    {
        if (!$this->is_empty()) {
            $this->front = ($this->front + 1) % $this->size;
            $element = $this->queue[$this->front];
            $this->queue[$this->front] = '';
            return $element;
        } else {
            throw new Exception("队列为空");
        }
    }

    # 判断队列是否为空
    public function is_empty()
    {
        return $this->rear == $this->front;
    }

    # 判断队列是否满的
    public function is_filled()
    {
        return ($this->rear + 1) % $this->size == $this->front;
    }

    public function get_queue()
    {
        return $this->queue;
    }
}

//声明栈
$queue = new queue(6);
$queue->push(5);
$queue->push(6);
$queue->push(7);
$queue->push(8);
$queue->push(9);

$queue->pop();
$queue->pop();
$queue->pop();

$queue->push(10);
$queue->push(11);
$queue->push(12);


print_r($queue->get_queue());


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992408.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号