forked from m9rco/algorithm-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackExample.php
More file actions
224 lines (211 loc) · 6.33 KB
/
StackExample.php
File metadata and controls
224 lines (211 loc) · 6.33 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* StackExample
*
* @author Pu ShaoWei <[email protected]>
* @date 2017/10/16
* @license MIT
* -------------------------------------------------------------
* [栈的定义]
* =======
* 栈(Stack):是限制在表的一端进行插入和删除操作的线性表。又称为后进先出LIFO (Last In First Out)或先进后出FILO (First In Last Out)线性表。
* 栈顶(Top):允许进行插入、删除操作的一端,又称为表尾。用栈顶指针(top)来指示栈顶元素。
* 栈底(Bottom):是固定端,又称为表头。
* 空栈:当表中没有元素时称为空栈。
* [栈的实现方式]
* =======
* - 硬堆栈:利用CPU中的某些寄存器组或类似的硬件或使用内存的特殊区域来实现。这类堆栈容量有限,但速度很快;
* - 软堆栈:这类堆栈主要在内存中实现。堆栈容量可以达到很大。在实现方式上,又有动态方式和静态方式两种
* -------------------------------------------------------------
* [定义]
* =======
* 线性表(Linear List) :是由n(n≧0)个数据元素(结点) [a1,a2, …an] 组成的有限序列。数据元素是一个抽象的符号,其具体含义在不同的情况下一般不同。
* 该序列中的所有结点具有相同的数据类型。其中数据元素的个数n称为线性表的长度。
* 当n=0时,称为空表。
* 当n>0时,将非空的线性表记作: (a1,a2,…an) a1称为线性表的第一个(首)结点,an称为线性表的最后一个(尾)结点。
* -------------------------------------------------------------
* [线性表顺序存储]
* =======
* 把线性表的结点按逻辑顺序依次存放在一组地址连续的存储单元里,用这种方法存储的线性表简称线性表。
* -------------------------------------------------------------
* [顺序存储的线性表的特点]
* =======
* - 线性表的逻辑顺序与物理顺序一致;
* - 数据元素之间的关系是以元素在计算机内“物理位置相邻”来体现。
* -------------------------------------------------------------
* @param array
*/
class StackExample
{
/**
* @var null 栈元素
*/
protected $elem;
/**
* @var null 下一个节点
*/
protected $next;
/**
* @var int 栈长度
*/
protected $length;
/**
* 初始化栈
* StackExample constructor.
*/
public function __construct()
{
$this->elem = $this->next = null;
$this->length = 0;
}
/**
* 判断栈是否空栈
*
* @return boolean 如果为空栈返回true,否则返回false
*/
protected function getIsEmpty()
{
return $this->elem ? false : true;
}
/**
* 将所有元素出栈
*
* @return array 返回所有栈内元素
*/
protected function getAllPopStack()
{
$result = array ();
if ($this->getIsEmpty()) {
return $result;
}
while ($this->elem != null) {
$result[] = $this->elem->elem;
$this->elem = $this->elem->next;
}
$this->length = 0;
return $result;
}
/**
* 返回栈内元素个数
*
* @return int
*/
protected function getLength()
{
return $this->length;
}
/**
* 元素进栈
*
* @param mixed $element 进栈元素值
* @return void
**/
protected function setPushStack($element)
{
$stack = new stdClass();
$stack->elem = $element;
$stack->next = $this->elem;
$this->elem = &$stack;
$this->length++;
}
/**
* 元素出栈
*
* @return boolean 出栈成功返回true,否则返回false
**/
protected function getPopStack()
{
if ($this->getIsEmpty()) {
return false;
}
$node = $this->elem;
$this->elem = $node->next;
$this->length--;
}
/**
* 仅返回栈内所有元素
*
* @return array 栈内所有元素组成的一个数组
*/
protected function getAllElem()
{
$result = array ();
if ($this->getIsEmpty()) {
return $result;
}
$node = $this->elem;
while ($node != null) {
$result[] = $node->elem;
$node = $node->next;
}
return $result;
}
/**
* 返回栈内某个元素的个数
*
* @param mixed $elem 待查找的元素的值
* @return int
**/
protected function getCountForElem($elem)
{
$result = $this->getAllElem();
$count = 0;
foreach ($result as $value) {
if ($elem === $value) {
$count++;
}
}
return $count;
}
/**
* 调试用请无视
*
* @param $name
* @param $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
$this->preDispatch(); // 调试用,请无视
return call_user_func_array(array ($this, $name), $arguments);
}
/**
* 调试用
*
* @param bool $isDebug
* @return bool
*/
protected function preDispatch($isDebug = true)
{
if (!$isDebug) {
return false;
}
$debug = debug_backtrace()[1];
$reflection = new ReflectionMethod($this, $debug['args'][0]);
$args = '';
if (isset($debug['args'][1])) {
foreach ($debug['args'][1] as &$value) {
if (is_array($value)) {
$args .= json_encode($debug['args'][1]) . ', ';
} else {
$args .= $value . ', ';
}
}
}
$args = trim($args, ', ');
echo "\t" . $reflection->getDocComment() . PHP_EOL;
echo "\t{$debug['args'][0]}({$args})\n" . PHP_EOL;
}
}
$echo = function ($str, $action) {
echo $str . "\t->\t" . var_export($action, true) . PHP_EOL;
echo "--------------------------- " . PHP_EOL;
};
//$stack = new StackExample();
//$stack->setPushStack('First');
//$stack->setPushStack('Second');
//$echo('返回栈内所有元素', $stack->getAllElem());
//$stack->setPushStack('Third');
//$echo('返回栈内所有元素', $stack->getAllElem());
//$stack->getPopStack();
//$echo('返回栈内所有元素', $stack->getAllElem());