-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.php
More file actions
94 lines (80 loc) · 1.96 KB
/
Copy pathStack.php
File metadata and controls
94 lines (80 loc) · 1.96 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/* Stack!!
* A stack is exactly what it sounds like. An element gets added to the top of
* the stack and only the element on the top may be removed. This is an example
* of an array implementation of a Stack. So an element can only be added/removed
* from the end of the array.
*/
// Functions: push, pop, peek, view, length
function consoleLog()
{
$args = func_get_args();
if (!$args) {
return;
}
$output = $args[0];
if (count($args) > 1) {
$output = call_user_func_array('sprintf', $args);
} else {
if (!is_scalar($output)) {
$output = var_export($output, true);
}
}
echo "$output \n";
}
class Stack
{
public $top = 0;
public $stack = [];
public function __construct()
{
$this->top = 0;
$this->stack = [];
}
public function push($value)
{
$this->stack[$this->top] = $value;
$this->top ++;
}
public function pop()
{
if($this->top == 0)
{
throw new \Exception('Stack is Empty');
}
$this->top --;
$result = $this->stack[$this->top];
unset($this->stack[$this->top]);
return $result;
}
public function size()
{
return count($this->stack);
}
public function peek()
{
return $this->stack[$this->top - 1];
}
//To see all the elements in the stack
public function view () {
for ($i = 0; $i < $this->top; $i++)
consoleLog($this->stack[$i]);
}
}
//Implementation
$myStack = new Stack();
$myStack->push(1);
$myStack->push(5);
$myStack->push(76);
$myStack->push(69);
$myStack->push(32);
$myStack->push(54);
consoleLog('size:'.$myStack->size());
consoleLog('peek:'.$myStack->peek());
consoleLog('pop:'.$myStack->pop());
consoleLog('peek:'.$myStack->peek());
consoleLog('pop:'.$myStack->pop());
consoleLog('peek:'.$myStack->peek());
$myStack->push(55);
consoleLog('peek:'.$myStack->peek());
$myStack->view();