May 2017
Intermediate to advanced
340 pages
8h 16m
English
We are now going to implement the queue data structure using a PHP array. We have already seen that we can use the array_push() function to add an element at the end of the array. In order to remove the first element of the array, we can use the array_shift() function of PHP, and for the peek function, we can use the current() function of PHP. Here is how the code will look, based on our discussion:
class AgentQueue implements Queue { private $limit; private $queue; public function __construct(int $limit = 20) { $this->limit = $limit; $this->queue = []; } public function dequeue(): string { if ($this->isEmpty()) { throw new UnderflowException('Queue is empty'); } else { return array_shift($this->queue); ...Read now
Unlock full access