cancio-labs/php-queue

Interface and an array-based implementation of the FIFO Queue data structure.

v1.1.0 2025-01-28 00:06 UTC

This package is auto-updated.

Last update: 2025-04-28 00:40:31 UTC


README

This tiny package contains an interface and an array-based implementation of the FIFO Queue data structure.

Interface

The Queue interface extends both Countable and IteratorAggragate interfaces, so it's possible to use a queue object inside the count function and foreach loop.

Methods

Method Description
enqueue Adds a new element to the back of the queue.
dequeue Removes and return the front element from the queue.
back Returns the back element of the queue.
front Returns the front element of the queue.
isEmpty Tests whether the queue is empty.
clear Removes all elements from the queue.
count Returns the number of elements of the queue.
getIterator Returns a QueueIterator.
toArray Transforms the queue into an array.

How to use it

use CancioLabs\Ds\Queue\Queue;

$queue = new Queue(['A', 'B']);

$queue->enqueue('C');
$queue->enqueue('D');

$queue->isEmpty(); // returns false
$queue->count(); // returns 4
count($queue) // returns 4

$queue->front(); // output 'A'
$queue->back(); // output 'D'
$queue->dequeue(); // output 'A'

$array = $queue->toArray(); // returns ['B', 'C', 'D']

foreach ($queue as $element) {
    // i=0: $element = 'B'
    // i=1: $element = 'C'
    // i=2: $element = 'D'
}

$queue->isEmpty(); // returns true 

Tests and Coverage

All tests are passing with no warnings and code coverage is 100%.