forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLinkedList.php
292 lines (250 loc) · 6.55 KB
/
SingleLinkedList.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
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* User: lide01
* Date: 2018/10/8 11:55
* Desc:
*/
namespace Algo_06;
/**
* 单链表
*
* Class SingleLinkedList
*
* @package Algo_06
*/
class SingleLinkedList
{
/**
* 单链表头结点(哨兵节点)
*
* @var SingleLinkedListNode
*/
public $head;
/**
* 单链表长度
*
* @var
*/
private $length;
/**
* 初始化单链表
*
* SingleLinkedList constructor.
*
* @param null $head
*/
public function __construct($head = null)
{
if (null == $head) {
$this->head = new SingleLinkedListNode();
} else {
$this->head = $head;
}
$this->length = 0;
}
/**
* 获取链表长度
*
* @return int
*/
public function getLength()
{
return $this->length;
}
/**
* 插入数据 采用头插法 插入新数据
*
* @param $data
*
* @return SingleLinkedListNode|bool
*/
public function insert($data)
{
return $this->insertDataAfter($this->head, $data);
}
/**
* 删除节点
*
* @param SingleLinkedListNode $node
*
* @return bool
*/
public function delete(SingleLinkedListNode $node)
{
if (null == $node) {
return false;
}
// 获取待删除节点的前置节点
$preNode = $this->getPreNode($node);
// 修改指针指向
$preNode->next = $node->next;
unset($node);
$this->length--;
return true;
}
/**
* 通过索引获取节点
*
* @param int $index
*
* @return SingleLinkedListNode|null
*/
public function getNodeByIndex($index)
{
if ($index >= $this->length) {
return null;
}
$cur = $this->head->next;
for ($i = 0; $i < $index; ++$i) {
$cur = $cur->next;
}
return $cur;
}
/**
* 获取某个节点的前置节点
*
* @param SingleLinkedListNode $node
*
* @return SingleLinkedListNode|bool
*/
public function getPreNode(SingleLinkedListNode $node)
{
if (null == $node) {
return false;
}
$curNode = $this->head;
$preNode = $this->head;
// 遍历找到前置节点 要用全等判断是否是同一个对象
// http://php.net/manual/zh/language.oop5.object-comparison.php
while ($curNode !== $node && $curNode != null) {
$preNode = $curNode;
$curNode = $curNode->next;
}
return $preNode;
}
/**
* 输出单链表 当data的数据为可输出类型
*
* @return bool
*/
public function printList()
{
if (null == $this->head->next) {
return false;
}
$curNode = $this->head;
// 防止链表带环,控制遍历次数
$listLength = $this->getLength();
while ($curNode->next != null && $listLength--) {
echo $curNode->next->data . ' -> ';
$curNode = $curNode->next;
}
echo 'NULL' . PHP_EOL;
return true;
}
/**
* 输出单链表 当data的数据为可输出类型
*
* @return bool
*/
public function printListSimple()
{
if (null == $this->head->next) {
return false;
}
$curNode = $this->head;
while ($curNode->next != null) {
echo $curNode->next->data . ' -> ';
$curNode = $curNode->next;
}
echo 'NULL' . PHP_EOL;
return true;
}
/**
* 在某个节点后插入新的节点 (直接插入数据)
*
* @param SingleLinkedListNode $originNode
* @param $data
*
* @return SingleLinkedListNode|bool
*/
public function insertDataAfter(SingleLinkedListNode $originNode, $data)
{
// 如果originNode为空,插入失败
if (null == $originNode) {
return false;
}
// 新建单链表节点
$newNode = new SingleLinkedListNode();
// 新节点的数据
$newNode->data = $data;
// 新节点的下一个节点为源节点的下一个节点
$newNode->next = $originNode->next;
// 在originNode后插入newNode
$originNode->next = $newNode;
// 链表长度++
$this->length++;
return $newNode;
}
/**
* 在某个节点前插入新的节点(很少使用)
*
* @param SingleLinkedListNode $originNode
* @param $data
*
* @return SingleLinkedListNode|bool
*/
public function insertDataBefore(SingleLinkedListNode $originNode, $data)
{
// 如果originNode为空,插入失败
if (null == $originNode) {
return false;
}
// 先找到originNode的前置节点,然后通过insertDataAfter插入
$preNode = $this->getPreNode($originNode);
return $this->insertDataAfter($preNode, $data);
}
/**
* 在某个节点后插入新的节点
*
* @param SingleLinkedListNode $originNode
* @param SingleLinkedListNode $node
*
* @return SingleLinkedListNode|bool
*/
public function insertNodeAfter(SingleLinkedListNode $originNode, SingleLinkedListNode $node)
{
// 如果originNode为空,插入失败
if (null == $originNode) {
return false;
}
$node->next = $originNode->next;
$originNode->next = $node;
$this->length++;
return $node;
}
/**
* 构造一个有环的链表
*/
public function buildHasCircleList()
{
$data = [1, 2, 3, 4, 5, 6, 7, 8];
$node0 = new SingleLinkedListNode($data[0]);
$node1 = new SingleLinkedListNode($data[1]);
$node2 = new SingleLinkedListNode($data[2]);
$node3 = new SingleLinkedListNode($data[3]);
$node4 = new SingleLinkedListNode($data[4]);
$node5 = new SingleLinkedListNode($data[5]);
$node6 = new SingleLinkedListNode($data[6]);
$node7 = new SingleLinkedListNode($data[7]);
$this->insertNodeAfter($this->head, $node0);
$this->insertNodeAfter($node0, $node1);
$this->insertNodeAfter($node1, $node2);
$this->insertNodeAfter($node2, $node3);
$this->insertNodeAfter($node3, $node4);
$this->insertNodeAfter($node4, $node5);
$this->insertNodeAfter($node5, $node6);
$this->insertNodeAfter($node6, $node7);
$node7->next = $node4;
}
}