-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.php
284 lines (260 loc) · 6.08 KB
/
function.php
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2018/11/28
* Time: 10:37
*/
/**
* 浏览器友好的变量输出,便于调试时候使用
*
* @param mixed $var 要输出查看的内容
* @param bool $echo 是否直接输出
* @param string $label 加上说明标签,如果有,这显示"标签名:"这种形式
* @param bool $strict 是否严格过滤
* @return string
*/
if (!function_exists('Dump')) {
function Dump()
{
$argc = func_get_args();
foreach ($argc as $value) {
Dumps($value);
}
}
function Dumps($var, $echo = true, $label = null, $strict = true)
{
$label = ($label === null) ? '' : rtrim($label) . ' ';
if (!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, true);
$output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
} else {
$output = $label . " : " . print_r($var, true);
}
} else {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
}
}
if ($echo) {
echo($output);
return null;
} else
return $output;
}
}
/**
* 调试方法
* @param array $data [description]
*/
function p($data)
{
echo "<pre>";
print_r($data);
echo "</pre>";
}
class Node
{
public $data = '';
public $next = null;
function __construct($data)
{
$this->data = $data;
}
//链表有几个节点
function countNode($head)
{
$cur = $head;
$i = 0;
while (!is_null($cur->next)) {
++$i;
$cur = $cur->next;
}
return $i;
}
//增加节点
function addNode($head, $data)
{
if (is_array($data)) {
foreach ($data as $k => $v) {
$this->addNode($head, $v);
}
} else {
$cur = $head;
while (!is_null($cur->next)) {
$cur = $cur->next;
}
$next = new Node($data);
$cur->next = $next;
}
}
// 插入$no 后
function insertNode($head, $data, $no)
{
if ($no > $this->countNode($head))
return false;
$cur = $head;
$new = new Node($data);
for ($i = 0; $i < $no; $i++) {
$cur = $cur->next;
}
$new->next = $cur->next;
$cur->next = $new;
}
//删除 第$no 个节点
function delNode($head, $no)
{
if ($no > $this->countNode($head))
return false;
$cur = $head;
for ($i = 0; $i < $no - 1; $i++) {
$cur = $cur->next;
}
$cur->next = $cur->next->next;
}
//根据 节点 value删除节点
function delNodeValue($head, $value)
{
$cur = $head;
$i = 0;
while (!is_null($cur->next)) {
$cur = $cur->next;
++$i;
if ($cur->data == $value) {
$this->delNode($head, $i);
}
}
return;
}
/**
* @param $head 链表
* @param $num 删除倒数的节点
*/
public function undelNode($head, $num)
{
$frist = $head;
$second = $head;
for ($i = 0; $i < $num; $i++) {
$frist = $frist->next;
}
while (!@is_null($frist->next)) {
$frist = $frist->next;
$second = $second->next;
}
$second->next = $second->next->next;
}
/**
* 反转链表
* @param $head
* @return unNodes
*/
public function unNode($head)
{
$cur = $head;
$i = 0;
$arr = [];
while (!is_null($cur->next)) {
++$i;
$cur = $cur->next;
$arr[] = $cur->data;
}
$arr = $this->unarr($arr);
$head = new unNodes(null);
$head->addNode($head, $arr);
return $head;
}
/**
* 数组反转
* @param $arr
* @return array|void
*/
public function unarr($arr)
{
if (!end($arr))
return;
$b = [];
$b[] = end($arr);
while (prev($arr)) {
$b[] = current($arr);
}
return $b;
}
/**
* 合并两个链表
* @param $node
* @param $data
*/
public function andNode($node, $data)
{
$cur = $node;
$sout = $data;
while (!is_null($cur->next)) {
$cur = $cur->next;
}
while (!is_null($sout->next)) {
$cur->next = $sout->next;
$cur = $cur->next;
$sout = $sout->next;
}
}
/**
* 回文链表
* @param $node
*/
public function Palindrome($node)
{
$cur = $node;
$sum = 0;
$i = 1;
$arr = [];
while (!is_null($cur->next)) {
$arr[] = $cur->next->data;
$sum = $sum + $cur->next->data * $i;
++$i;
$cur = $cur->next;
}
$i = 1;
$b[] = end($arr);
$sum = $sum - current($arr) * $i;
++$i;
while (prev($arr)) {
$sum = $sum - current($arr) * $i;
p(current($arr));
p($i);
echo '<hr>';
++$i;
}
if ($sum == 0)
echo 'true';
else
echo 'false';
}
//遍历链表
function showNode($head)
{
$cur = $head;
while (!is_null($cur->next)) {
$cur = $cur->next;
echo $cur->data, '<br>';
}
}
}
class Tree
{
public $key;
public $parent;
public $left;
public $right;
public function __construct($key)
{
$this->key = $key;
$this->parent = NULL;
$this->left = NULL;
$this->right = NULL;
}
}