数据结构之php实现队列

图片

在上一次的文章中, 我们通过数组实现了栈, 在讲解数组的过程中, 也提到了一点, 就是栈是线性结构, 相比数组, 队列对应的操作是数组的子集。在今天的文章里,栈和队列也是属于线性结构。

那什么是队列呢? 我们一起来看下定义:

一、概念

队列,又称为伫列(queue),计算机科学中的一种抽象资料型别,是先进先出(FIFO, First-In-First-Out)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。

维基百科

通过定义我们可以知道,先进先出(FIFO)的线性数据结构,通常可以通过数组或者链表进行实现。比如我们在进行买票的时候,优先排队的人,自然得到有限处理,在我们现实生活中还有很多,如乘公交,结账,检票等等。

图片

(图片来之网络 -- 如有侵权,请联系删除)

大概讲了一些队列的概念和一些现实生活中的一些案例, 我们来看一下队列在计算机系统中”她”是什么样子的吧

图片

(队列示意图)

二、代码实现

前面说过,实现的方式可以通过链表和数组实现,这次的代码我们只是通过数组进行实现,如果有兴趣的同学,可以自行通过链表进行一次实现

ArrayStructure.php

<?php

class ArrayStructure
{
    // 数组实际元素
    private $size = 0;

    // 数组的容量大小
    private $capacity = 0;

    // 用于存放数据
    private $data = [];

    /**
     * ArrayStruct constructor.
     *
     * @param int $capacity 数组容量大小
     */
    public function __construct($capacity = 10)
    {
        $this->capacity = $capacity;
    }

    /**
     * Notes: 获取数组实际元素个数
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:12
     *
     * @return int
     */
    public function getSize(): int
    {
        return $this->size;

    }

    /**
     * Notes: 扩容
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 17:08
     *
     * @param $factor
     */
    protected function resize($factor)
    {
        $this->capacity = $factor * $this->capacity;

    }

    /**
     * Notes: 获取数组的容量
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:14
     *
     * @return int
     */
    public function getCapacity(): int
    {
        return $this->capacity;

    }

    /**
     * Notes: 数组是否为空
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:16
     *
     * @return bool
     */
    public function isEmpty(): bool
    {
        return $this->size === 0;

    }

    /**
     * Notes: 往数组指定位置插入数据
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:27
     *
     * @param int $index 需要插入的下标/索引
     * @param int $ele   需要插入的元素
     */
    public function add(int $index, $ele): void
    {
        // 如果当前的实际大小 等于 当前容量, 则不可以进行插入
        try {
            if ($this->size === $this->capacity) {
                throw new Exception("添加失败, Array 已经到达最大容量");
            }

            if ($index < 0 || $index > $this->size) {
                throw new Exception("添加失败, index require >= 0 and <= " . $this->size);
            }

            /**
             * 从最后一个元素开始进行遍历, 直到下标 为 当前输入的 下标, 终止
             * 每次将当前遍历的数值 往后 移动一位
             */
            for ($i = $this->size - 1; $i >= $index; $i --) {
                $this->data[$i + 1] = $this->data[$i];
            }

            // 目前当前下标还是存在之前的元素, 因为当前元素已经移动到后面一位, 所以直接覆盖就好
            $this->data[$index] = $ele;

            // 维护当前实际大小
            $this->size ++;
        }
        catch (Exception $e) {
            echo $e->getMessage();
        }

    }

    /**
     * Notes: 向所有元素后添加一个元素
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:19
     */
    public function addLast($element): void
    {
        $this->add($this->size, $element);

    }

    /**
     * Notes: 向第一位添加一个元素
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:19
     */
    public function addFirst(int $element): void
    {
        $this->add(0, $element);

    }

    /**
     * Notes: 数组转字符串
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 15:50
     *
     * @return string
     */
    public function toString(): string
    {
        $str = 'Array: size = ' . $this->size . ',' . 'capacity = ' . $this->capacity . PHP_EOL;
        $str .= '[';
        for ($i = 0; $i < $this->size; $i ++) {
            $str .= $this->data[$i];
            if ($i != $this->size - 1) {
                $str .= ',';
            }
        }

        $str .= ']';
        return $str . PHP_EOL;

    }

    /**
     * Notes: 获取指定下标位置的元素
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:13
     *
     * @param int $index
     *
     * @return int
     */
    public function get($index)
    {
        try {
            if ($index < 0 || $index >= $this->size) {
                throw new Exception("获取失败, index require >= 0 and < " . $this->size);
            }

            return $this->data[$index];
        }
        catch (Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * Notes: 获取最后一个
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 15:48
     *
     * @return int
     */
    public function getLast()
    {
        return $this->get($this->size - 1);
    }

    /**
     * Notes: 获取第一个
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 15:48
     *
     * @return int
     */
    public function getFirst()
    {
        return $this->get(0);
    }

    /**
     * Notes: 修改指定下标位置的元素为 ele
     * User: think abel
     * Date: 2021/1/9 0009
     * Time: 12:04
     *
     * @param int $index
     * @param int $ele
     *
     * @return string
     */
    public function set(int $index, int $ele)
    {
        try {
            if ($index < 0 || $index >= $this->size) {
                throw new Exception("获取失败, index require >= 0 and < " . $this->size);
            }

            $this->data[$index] = $ele;
        }
        catch (Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * Notes: 删除指定位置上的元素
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:19
     *
     * @param int $index
     *
     * @return int|string
     */
    public function remove(int $index): int
    {
        try {
            if ($index < 0 || $index >= $this->size) {
                throw new Exception("移除失败, index require >= 0 and < " . $this->size);
            }

            $return = $this->data[$index];

            for ($i = $index + 1; $i < $this->size; $i ++) {
                $this->data[$i - 1] = $this->data[$i];
            }

            $this->size --;
            return $return;
        }
        catch (Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * Notes: 删除第一个
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:39
     *
     * @return int
     */
    public function removeFirst(): int
    {
        try {
            return $this->remove(0);
        }
        catch (Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * Notes: 删除最后一个
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:39
     *
     * @return int
     */
    public function removeLast()
    {
        try {
            return $this->remove($this->size - 1);
        }
        catch (Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * Notes: 如果有元素, 就删除
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:44
     *
     * @param int $ele
     *
     * @return bool
     */
    public function removeElement(int $ele): bool
    {
        $index = $this->find($ele);
        if ($index != - 1) {
            $this->remove($index);
        }

    }

    /**
     * Notes: 是否包含元素
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:22
     *
     * @param int $ele
     *
     * @return bool
     */
    public function contains(int $ele): bool
    {
        for ($i = 0; $i < $this->size; $i ++) {
            if ($this->data[$i] == $ele) {
                return true;
            }
        }

        return false;

    }

    /**
     * Notes: 获取当前元素的索引
     * Author: PhpStorm
     * Date: 2021/1/8 0008
     * Time: 16:22
     *
     * @param int $ele
     *
     * @return int
     */
    public function find(int $ele): int
    {
        for ($i = 0; $i < $this->size; $i ++) {
            if ($this->data[$i] == $ele) {
                return $i;
            }
        }

        return - 1;

    }
}

Queue.php

<?php

/**

 * Created by : PhpStorm

 * User: think abel

 * Date: 2021/1/10 0010

 * Time: 16:36

 */



interface Queue
{
    public function enqueue($element);

    public function dequeue();

    public function getFront();

    public function getSize();

    public function isEmpty();
}

ArrayQueue.php

<?php
/**
 * Created by : PhpStorm
 * User: think abel
 * Date: 2021/1/10 0010
 * Time: 16:38
 */

include "Queue.php";
include "ArrayStructure.php";

class ArrayQueue implements Queue
{

    private $array;

    public function __construct(int $capacity)
    {
        $this->array = new ArrayStructure($capacity);
    }

    /**
     * Notes: 入队
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 16:42
     *
     * @param $element
     */
    public function enqueue($element)
    {
        $this->array->addLast($element);
    }

    /**
     * Notes: 出队
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 16:42
     *
     * @return int
     */
    public function dequeue()
    {
        return $this->array->removeFirst();
    }

    /**
     * Notes: 获取队首
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 16:42
     *
     * @return int
     */
    public function getFront()
    {
        return $this->array->getFirst();
    }

    /**
     * Notes: 获取实际元素个数
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 16:42
     *
     * @return int
     */
    public function getSize()
    {
       return $this->array->getSize();
    }

    /**
     * Notes:获取队列的容量
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 16:42
     *
     * @return int
     */
    public function getCapacity()
    {
        return $this->array->getCapacity();
    }

    /**
     * Notes: 是否为空
     * User: think abel
     * Date: 2021/1/10 0010
     * Time: 16:43
     *
     * @return bool
     */
    public function isEmpty()
    {
       return $this->array->isEmpty();
    }

    public function toString()
    {
        $str = 'Queue: front [';
        for ($i = 0; $i < $this->array->getSize(); $i ++) {
            $str .= $this->array->get($i);
            if ($i != $this->array->getSize() - 1) {
                $str .= ", ";
            }
        }

        $str .= "] tail";
        return $str;
    }
}

index.php

<?php
include('ArrayQueue.php');
$arrayStack = new ArrayQueue(10);
for ($i = 0; $i < 10; $i ++) {
    $arrayStack->enqueue($i);
    echo PHP_EOL;
    print_r($arrayStack->toString());
}

echo PHP_EOL;
$arrayStack->dequeue();
echo $arrayStack->toString();
echo PHP_EOL;
$front = $arrayStack->getFront();
echo $front;

我们来看下,队列操作 出队 和 入队的示意图
图片

(队列出入队示意图)

三、简单复杂度分析****

O: 描述是算法的运行时间 和 输入数据之间的关系 — 程序运行时间 和 数数据 成线性关系 O(n)

ArrayQueue
enqueue(e)   O(1) // 如果是触发了扩容, 通过均摊复杂度来分析的话,依然是O(1)
dequeue()    O(n) // 为什么是O(n), 因为出队一个, 其他的元素要向前移动一位
getFront()   O(1)
getSize()    O(1)
isEmpty(1)   O(1)

四、思考
通过上面的简单时间复杂度分析, 数组队列存在一个问题,相信大家也能发现。出队复杂度是O(n)

仅仅一个出队, 复杂度确实 O(n)的, 就不能像入队一样为O(1)吗?我们带着这个问题, 思考一下. 能不能让他不移动, 在每次出队的时候, 我们维护一下 front 的位置不就变成 O(1) 了吗。说做就做,先看下流程示意图

图片

(队列为空时)

队列为空时, 队首 和 队尾 是在同一个位置, 这也就说明, 如果** front == rear 的时候, 队列为空**。

图片

图片

(a1 出队)

队列中 a1 出队, 我们进行维护 Front 进行 +1 操作, 这时的 Front 到了a2, 一次类推。
图片

(a13 入队)

队列中a13入队的时候, 我们进行维护Rear, 进行移动, Rear的计算公式是 (队尾的下标 + 1) % 队列的长度。这个时候,比如在进来一个元素,队列长度为13, 目前队尾的下标为1, 队首的为2, 我们通过 计算公式 (1 + 1) % 13 = 2。这个时候是不是 队尾和队首相等了, 我们前面讲到, 队首 == 队尾 的时候,代表队列为空。但是目前是满队列了,所以这个时候就要做下处理,如果 (队尾的下标 + 1) % 队列的长度 == 队首,说明满队列。** 所以也就是我们 需要有意的浪费一个空间, 也就是说,我们在申请创建队列的时候,就需要在用户传递的 容量上 进行 + 1.**

这时我们在分析下复杂度

LoopQueue
enqueue(e)   O(1) // 如果是触发了扩容, 通过均摊复杂度来分析的话,依然是O(1)
dequeue()    O(1) // 如果是触发了扩容, 通过均摊复杂度来分析的话,依然是O(1)
getFront()   O(1)
getSize()    O(1)
isEmpty(1)   O(1)

代码我就不贴了, 如果有需要的话, 可以访问一下数据仓库

gitee.com/thinkAbel/data-structure

排版如果有问题的话, 大家可以关注一下公众号. 我们一起学习成长.

数据结构之php实现队列

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!