Skip to content

Commit cc55bb6

Browse files
committed
Add new Stack class
1 parent 740530d commit cc55bb6

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/TgUtils/Stack.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace TgUtils;
4+
5+
class Stack {
6+
7+
protected $stack;
8+
9+
public function __construct(...$values) {
10+
$this->stack = array();
11+
foreach ($values AS $value) {
12+
$this->push($value);
13+
}
14+
}
15+
16+
public function isEmpty() {
17+
return count($this->stack) == 0;
18+
}
19+
20+
public function size() {
21+
return count($this->stack);
22+
}
23+
24+
public function push($value) {
25+
if ($value != NULL) array_push($this->stack, $value);
26+
else throw new \Exception('Cannot push NULL element to stack');
27+
}
28+
29+
public function pop() {
30+
if (!$this->isEmpty()) {
31+
return array_pop($this->stack);
32+
}
33+
return NULL;
34+
}
35+
36+
public function peek() {
37+
if (!$this->isEmpty()) {
38+
return $this->stack[count($this->stack)-1];
39+
}
40+
return NULL;
41+
}
42+
}
43+

0 commit comments

Comments
 (0)