-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.php
More file actions
44 lines (36 loc) · 891 Bytes
/
ArrayStack.php
File metadata and controls
44 lines (36 loc) · 891 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
44
<?php
namespace Zubs\Dsa\Stack;
use OverflowException, UnderflowException;
/**
* An implementation of the Stack data structure using PHP arrays.
*/
class ArrayStack extends Base
{
/**
* @param int $limit
*/
public function __construct(int $limit)
{
$this->limit = $limit;
}
public function push(string $data): bool
{
if (count($this->stack) < $this->limit) {
array_push($this->stack, $data);
return true;
} else throw new OverflowException('Stack is full');
}
public function pop(): string
{
if ($this->isEmpty()) throw new UnderflowException('Stack is empty');
else return array_pop($this->stack);
}
public function top(): string
{
return end($this->stack);
}
public function isEmpty(): bool
{
return empty($this->stack);
}
}