Repository navigation
Expand file tree
/
Copy pathsingleLinkedList.php
More file actions
175 lines (164 loc) · 3.83 KB
/
Copy pathsingleLinkedList.php
File metadata and controls
175 lines (164 loc) · 3.83 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
<?php
namespace SingleLinedList;
require Node.php;
/**
* 实现单链表
*/
class SingleLinedList
{
/**
* 哨兵节点 - 头结点
*
* @var Node
*/
private $head = null;
/**
* 链表长度
*/
private $length;
/**
* 初始化单链表
*
* @param null $head 头结点
*/
public function __construct($head = null)
{
// 空链表中生成头结点
if ($head == null) {
$this->head = new Node();
} else { // 已有结点生成单链表
$this->head = $head;
}
$this->length = 0;
}
/**
* 获取链表长度
*
* @return int
*/
public function getLength()
{
return $this->length;
}
/**
* 采用头插法 生成倒序链表
*
* @param mixed $data
* @return Node
*/
public function headInsert($data)
{
return $this->insertDataAfter($this->head, $data);
}
/**
* 在某个节点后插入新的节点
*
* @param Node $originNode 要插入节点
* @param mixed $data 数据
* @return Node
*/
public function insertDataAfter(Node $originNode, $data)
{
if (empty($originNode)) {
return false;
}
$newNode = new Node();
$newNode->setData($data);
/*
* 将新的节点的后续指针指向要插入的节点的后续指针, 避免指针丢失
* 若先将 originNode->next = newNode newNode->next = originNode->next
* 等同于 newNode->next = originNode->next = newNode 指向newNode
*/
$newNode->setNext($originNode->getNext());
$originNode->setNext($newNode);
$this->length++;
return $newNode;
}
/**
* 在某个节点前插入新的节点
*
* @param Node $originNode
* @param mixed $data
* @return void
*/
public function insertDataBefore(Node $originNode, $data)
{
if (empty($originNode)) {
return false;
}
$preNode = $this->getPreNode($originNode);
return $this->insertDataAfter($preNode, $data);
}
/**
* 获取某个节点的前置节点
*
* @param Node $node
* @return void
*/
public function getPreNode(Node $node)
{
if (empty($node)) {
return false;
}
$curNode = $this->head;
$preNode = $this->head;
while ($curNode !== $node && $curNode != null) {
$preNode = $curNode;
$curNode = $curNode->next;
}
return $preNode;
}
/**
* 在某个节点后插入新的节点
*
* @param Node $originNode
* @param Node $newNode
* @return void
*/
public function insertNodeAfter(Node $originNode, Node $newNode)
{
if (empty($originNode)) {
return false;
}
$newNode->setNext($originNode->getNext());
$originNode->setNext($newNode);
$this->length++;
return true;
}
/**
* 打印链表
*
* @return void
*/
public function printList()
{
if (null === $this->head->getNext()) {
return false;
}
$curNode = $this->head;
$len = $this->length;
while ($curNode->getNext() !== null && $len--) {
echo $curNode->getNext()->getData() . '-> ';
$curNode = $curNode->getNext();
}
echo 'NULL' . PHP_EOL;
return true;
}
/**
* 删除结点
*
* @param Node $node
* @return void
*/
public function delete(Node $node)
{
if (empty($node)) {
return false;
}
$preNode = $this->getPreNode($node);
$preNode->setNext($node->getNext());
unset($node);
$this->length--;
return true;
}
}