This tiny package contains an interface and an array-based implementation of the FIFO Queue data structure.
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.
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. |
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
All tests are passing with no warnings and code coverage is 100%.