-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathLinkedListQueue.php
48 lines (39 loc) · 993 Bytes
/
LinkedListQueue.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
44
45
46
47
48
<?php
namespace DataStructure\Queue;
use DataStructure\LinkedList\LinkedList;
class LinkedListQueue implements QueueInterface
{
private $limit;
private $queue;
public function __construct(int $limit = 0)
{
$this->limit = $limit;
$this->queue = new LinkedList();
}
public function isEmpty()
{
return $this->queue->getSize() == 0;
}
public function peek()
{
return $this->queue->getNthNode(0)->data;
}
public function enqueue(string $item)
{
if ($this->queue->getSize() < $this->limit) {
$this->queue->insert($item);
} else {
throw new \OverflowException('queue is full');
}
}
public function dequeue()
{
if ($this->isEmpty()) {
throw new \UnderflowException('queue is empty');
} else {
$lastItem = $this->peek();
$this->queue->deleteFirst();
return $lastItem;
}
}
}