File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments