Skip to content

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

License

Notifications You must be signed in to change notification settings

canciolabs/php-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CancioLabs / DS / PHP-Queue

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%.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages