当前位置:首页 > PHP

php实现环形队列

2026-02-16 12:09:02PHP

PHP实现环形队列的方法

环形队列是一种特殊的队列数据结构,它通过固定大小的数组实现,利用模运算实现循环利用空间。以下是PHP实现环形队列的几种方法:

使用数组和指针实现

通过维护队首(front)和队尾(rear)指针以及当前元素数量(count)来实现环形队列:

php实现环形队列

class CircularQueue {
    private $queue;
    private $capacity;
    private $front;
    private $rear;
    private $count;

    public function __construct($capacity) {
        $this->queue = array_fill(0, $capacity, null);
        $this->capacity = $capacity;
        $this->front = 0;
        $this->rear = -1;
        $this->count = 0;
    }

    public function enqueue($item) {
        if ($this->isFull()) {
            throw new RuntimeException("Queue is full");
        }
        $this->rear = ($this->rear + 1) % $this->capacity;
        $this->queue[$this->rear] = $item;
        $this->count++;
    }

    public function dequeue() {
        if ($this->isEmpty()) {
            throw new RuntimeException("Queue is empty");
        }
        $item = $this->queue[$this->front];
        $this->front = ($this->front + 1) % $this->capacity;
        $this->count--;
        return $item;
    }

    public function peek() {
        if ($this->isEmpty()) {
            throw new RuntimeException("Queue is empty");
        }
        return $this->queue[$this->front];
    }

    public function isEmpty() {
        return $this->count == 0;
    }

    public function isFull() {
        return $this->count == $this->capacity;
    }

    public function size() {
        return $this->count;
    }
}

使用SplFixedArray优化性能

对于需要更高性能的场景,可以使用SplFixedArray代替普通数组:

php实现环形队列

class CircularQueueSpl {
    private $queue;
    private $capacity;
    private $front = 0;
    private $rear = -1;
    private $count = 0;

    public function __construct($capacity) {
        $this->queue = new SplFixedArray($capacity);
        $this->capacity = $capacity;
    }

    // 其他方法与前面示例类似
    // ...
}

实现环形缓冲区的变体

对于需要覆盖旧数据的场景,可以修改enqueue方法实现自动覆盖:

public function enqueue($item) {
    $this->rear = ($this->rear + 1) % $this->capacity;
    $this->queue[$this->rear] = $item;
    if ($this->count < $this->capacity) {
        $this->count++;
    } else {
        $this->front = ($this->front + 1) % $this->capacity;
    }
}

使用位运算优化模运算

当队列容量为2的幂次方时,可以使用位运算代替模运算提高性能:

public function __construct($capacity) {
    if (($capacity & ($capacity - 1)) != 0) {
        throw new InvalidArgumentException("Capacity must be a power of 2");
    }
    $this->queue = array_fill(0, $capacity, null);
    $this->capacity = $capacity;
    $this->mask = $capacity - 1;
}

public function enqueue($item) {
    if ($this->isFull()) {
        throw new RuntimeException("Queue is full");
    }
    $this->rear = ($this->rear + 1) & $this->mask;
    $this->queue[$this->rear] = $item;
    $this->count++;
}

以上实现提供了环形队列的基本功能,可以根据具体需求进行扩展和优化。实际应用中,环形队列常用于需要固定大小缓冲区的场景,如音频处理、网络数据包缓冲等。

标签: 环形队列
分享给朋友:

相关文章

vue队列实现播放

vue队列实现播放

Vue 队列播放实现方法 在 Vue 中实现队列播放功能,可以通过以下方式完成: 使用数组管理队列 定义一个数组来存储待播放的项目,利用数组的 push 和 shift 方法实现队列的先进先出特性。…

react实现消息队列

react实现消息队列

实现消息队列的基本思路 在React中实现消息队列通常需要结合状态管理工具(如Redux、Context API)或自定义Hook来管理消息的存储、消费和清理。消息队列的核心功能包括消息的入队、出队、…

css环形图制作

css环形图制作

使用CSS制作环形图 CSS环形图可以通过多种方法实现,包括使用border-radius、conic-gradient以及SVG等技术。以下是几种常见的方法: 使用border-radius和伪元…

php使用redis实现队列

php使用redis实现队列

PHP 使用 Redis 实现队列 Redis 是一个高性能的键值存储系统,支持多种数据结构,如字符串、哈希、列表、集合等。利用 Redis 的列表(List)数据结构可以轻松实现队列功能。以下是实现…

js 实现队列

js 实现队列

队列的基本概念 队列是一种先进先出(FIFO)的数据结构,支持从尾部插入元素(入队)和从头部移除元素(出队)。以下是 JavaScript 中实现队列的几种方法。 使用数组实现队列 数组…

队列的实现js

队列的实现js

队列的基本概念 队列是一种先进先出(FIFO)的数据结构,支持在队尾插入元素(入队)和在队头删除元素(出队)。JavaScript中可通过数组或链表实现队列。 基于数组的实现 数组的push和shi…