-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQueue.php
More file actions
89 lines (80 loc) · 1.64 KB
/
ArrayQueue.php
File metadata and controls
89 lines (80 loc) · 1.64 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
require_once __DIR__ . '/../array/MyArray.php';
require_once __DIR__ . '/QueueInterface.php';
class ArrayQueue implements QueueInterface
{
private $array;
/**
* 构造函数,初始化
* ArrayQueue constructor.
* @param int $capacity
*/
public function __construct(int $capacity = 10)
{
$this->array = new MyArray($capacity);
}
/**
* 获取队列大小
* @return int
*/
public function getSize(): int
{
return $this->array->getSize();
}
/**
* 判断队列是否为空
* @return bool
*/
public function isEmpty(): bool
{
return $this->array->isEmpty();
}
/**
* 获取队列容量
* @return int
*/
public function getCapacity(): int
{
return $this->array->getCapacity();
}
/**
* 添加队列
* @param $e
*/
public function enqueue($e): void
{
$this->array->addLast($e);
}
/**
* 出队
* @return mixed
*/
public function dequeue()
{
return $this->array->removeFirst();
}
/**
* 获取队首
* @return mixed
*/
public function getFront()
{
return $this->array->getFirst();
}
/**
* 格式化输出数组信息
* @return string
*/
public function __toString():string
{
$ret = 'Queue:front [';
for ($i = 0 ; $i < $this->getSize(); $i++) {
$ret .= $this->array->get($i);
if ($i != $this->getSize() -1) {
$ret .= ', ';
}
}
$ret .= '] tail' . PHP_EOL;
return $ret;
}
}