-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathQueue.php
More file actions
43 lines (33 loc) · 869 Bytes
/
Queue.php
File metadata and controls
43 lines (33 loc) · 869 Bytes
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 srcPhp;
use srcPhp\LinkedList;
class Queue {
private $n;
private $queue;
public function __construct(int $n = 20) {
$this->n = $n;
$this->queue = new LinkedList();
}
public function dequeue(): string {
if ($this->isEmpty()) {
throw new UnderflowException('Queue is empty');
} else {
$lastItem = $this->peek();
$this->queue->removeFirst();
return $lastItem;
}
}
public function enqueue(string $newItem) {
if ($this->queue->getSize() < $this->n) {
$this->queue->insertLast($newItem);
} else {
throw new OverflowException('Queue is full');
}
}
public function peek(): string {
return $this->queue->getAt(1)->data;
}
public function isEmpty(): bool {
return $this->queue->getSize() == 0;
}
}