-
Notifications
You must be signed in to change notification settings - Fork 143
/
ArrStack.php
47 lines (40 loc) · 914 Bytes
/
ArrStack.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
<?php
namespace DataStructure\Stack;
class ArrStack implements StackInterface
{
private $stack;
private $limit;
public function __construct(int $limit = 20)
{
$this->limit = $limit;
$this->stack = [];
}
public function __get($val)
{
return $this->$val;
}
public function push(string $data = null)
{
if (count($this->stack) < $this->limit) {
array_push($this->stack, $data);
} else {
throw new \OverflowException('stack is overflow');
}
}
public function pop()
{
if ($this->isEmpty()) {
throw new \UnderflowException('stack is empty');
} else {
return array_pop($this->stack);
}
}
public function isEmpty()
{
return empty($this->stack);
}
public function top()
{
return end($this->stack);
}
}