Last active
May 29, 2016 08:51
-
-
Save esase/7a0c17065424b5c9f63b9e6e06dfd2ee to your computer and use it in GitHub Desktop.
Queue [data type]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Queue | |
{ | |
/** | |
* Items | |
* | |
* @var array | |
*/ | |
protected $items = []; | |
/** | |
* Enqueue | |
* | |
* @param mixed $value | |
* @return void | |
*/ | |
public function enqueue($value) | |
{ | |
$this->items[] = $value; | |
} | |
/** | |
* Dequeue | |
* | |
* @return mixed | |
* @throws Exception | |
*/ | |
public function dequeue() | |
{ | |
if (!$this->items) { | |
throw new Exception('There are no any items in queue'); | |
} | |
$currentItemValue = current($this->items); | |
unset($this->items[key($this->items)]); | |
return $currentItemValue; | |
} | |
} | |
$queue = new Queue(); | |
// fill the queue | |
foreach (range(0, 15) as $value) { | |
$queue->enqueue($value); | |
} | |
// extract from the queue | |
foreach (range(0, 15) as $value) { | |
echo $queue->dequeue() . '<br>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment