-
Notifications
You must be signed in to change notification settings - Fork 143
/
ArrQueue.php
43 lines (36 loc) · 836 Bytes
/
ArrQueue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace DataStructure\Queue;
class ArrQueue implements QueueInterface
{
private $queue;
private $limit;
public function __construct(int $limit = 0)
{
$this->limit = $limit;
$this->queue = [];
}
public function isEmpty()
{
return empty($this->queue);
}
public function dequeue()
{
if ($this->isEmpty()) {
throw new \UnderflowException('queue is empty');
} else {
array_shift($this->queue);
}
}
public function enqueue(string $item)
{
if (count($this->queue) >= $this->limit) {
throw new \OverflowException('queue is full');
} else {
array_push($this->queue, $item);
}
}
public function peek()
{
return current($this->queue);
}
}