This repository was archived by the owner on Oct 16, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +63
-1
lines changed
Expand file tree Collapse file tree 3 files changed +63
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /* Stack Implementation in PHP */
3+
4+ //Stack Class
5+ class Stack{
6+ private $ _items = array ();
7+
8+ public function push ($ value = NULL ) {
9+ array_push ($ this ->_items , $ value );
10+ }
11+
12+ public function pop () {
13+ return array_pop ($ this ->_items );
14+ }
15+
16+ public function peek () {
17+ return end ($ this ->_items );
18+ }
19+
20+ public function size () {
21+ return count ($ this ->_items );
22+ }
23+
24+ public function isEmpty () {
25+ return empty ($ this ->_items );
26+ }
27+
28+ public function printAll () {
29+ foreach ($ this ->_items as $ item ) {
30+ var_dump ($ item );
31+ }
32+ }
33+ }
34+
35+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+ /***************** Testing the Stack ***************/
3+ include ('Stack.php ' );
4+
5+ $ stack = new Stack ();
6+ $ stack ->push (10 );
7+ $ stack ->push (15 );
8+
9+ echo $ stack ->isEmpty () ? 'Empty ' : 'Not Empty ' ;
10+ echo PHP_EOL ;
11+
12+ echo $ stack ->peek ();
13+ echo PHP_EOL ;
14+
15+ echo $ stack ->pop ();
16+ echo PHP_EOL ;
17+
18+ echo $ stack ->pop ();
19+ echo PHP_EOL ;
20+
21+ echo $ stack ->pop ();
22+ echo PHP_EOL ;
23+
24+ echo $ stack ->isEmpty () ? 'Empty ' : 'Not Empty ' ;
25+ echo PHP_EOL ;
26+
27+ ?>
Original file line number Diff line number Diff line change 88- [ Introduction to PHP] ( #introduction )
99- [ Data Structure] ( ./Data%20Structure/ )
1010 - Linked List
11- - Stack
11+ - [ Stack] ( ./Data%20Structure/Stack )
1212 - Queue
1313 - Binary Search Tree (BST)
1414 - Heap
You can’t perform that action at this time.
0 commit comments