Skip to content

Commit a3f5860

Browse files
committed
Stack code added
1 parent 424b27b commit a3f5860

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

Data Structure/Stack/Stack.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
?>

Data Structure/Stack/test.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
?>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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

0 commit comments

Comments
 (0)