0%

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
<?php
class Node {
private $data;
private $next;

public function getData() {
return $this->data;
}

public function setData($data) {
$this->data = $data;
return true;
}

public function getNext() {
return $this->next;
}

public function setNext($next) {
$this->next = $next;
return true;
}
}

/**
* 链表类
*/
class Link {
private $size = 0;
private $first;
private $last;

/**
* 获取链表长度
*/
public function getLength() {
return $this->size;
}

/**
* 链表中插入第一个元素的时候,头和尾部都是同一个元素
*/
public function oneNode(string $element) {
$this->first = new Node;
$this->first->setData($element);
$this->last = $this->first;
}

/**
* 当只有一个元素的时候,删除$fist和$last
*/
public function clear() {
$this->first = $this->last = null;
}

/**
* 头节点插入
*/
public function addHead(string $element) {
if ($this->size == 0) {
$this->oneNode($element);
} else {
$node = new Node;
$node->setData($element);
$node->setNext($this->first);
$this->first = $node;
}
$this->size++;
return true;
}

/**
* 尾节点插入
*/
public function addTail(string $element) {
if ($this->size == 0) {
$this->oneNode($element);
} else {
$node = new Node();
$node->setData($element);
$this->last->setNext($node);
$this->last = $node;
}

$this->size++;
}

/**
* 中间节点插入
*/
public function add(int $index, string $element) {
if ($index <= $this->size) {
if ($this->size == 0) {
oneNode($element);
} elseif ($index == 0) {
$this->addHead($element);
} elseif ($index == $this->size) {
$this->addTail($element);
} else {
$tmp = $this->get($index - 1);
$node = new Node;
$node->setData($element);
$node->setNext($tmp->getNext());
$tmp->setNext($node);
}
$this->size++;
} else {
throw new \Exception("插入位置无效或超出链表长度");
}
}

/**
* 获取指定位置的元素
*/
public function get(int $index) {
$tmp = $this->first;
for ($i = 0; $i < $index - 1; $i++) {
$tmp = $tmp->getNext();
}
return $tmp;
}

/**
* 删除头节点
*/
public function deleteFirst() {
if ($this->size == 0) {
throw new \Exception("空链表,无元素可删除");
} elseif ($this->size == 1) {
$this->clear();
} else {
$tmp = $this->first;
$this->first = $tmp->getNext();
$this->size--;
}
}

/**
* 删除尾节点
*/
public function deleteLast() {
if ($this->size == 0) {
throw new \Exception("空链表,无元素可删除");
} elseif ($this->size == 1) {
$this->clear();
} else {
$tmp = $this->get($this->size - 1);
$tmp->setNext(null);
$this->size--;
}
}

/**
* 删除指定节点
*/
public function deleteIndex(int $index) {
if ($this->size == 0) {
throw new \Exception("空链表,无元素可删除");
} elseif ($this->size == 1) {
$this->clear();
} else {
$tmp = $this->get($index - 1);
$tmp->setNext($tmp->getNext()->getNext());
$this->size--;
}
}

/**
* 反转节点
*/
public function receve() {
if ($this->size < 2) {
return true;
} else {
$tmp = $this->first;
$last = $tmp;
$next = $this->first->getNext();
for($i = 0; $i < $this->size - 1; $i++) {
$nextNext = $next->getNext();
$next->setNext($tmp);
$tmp = $next;
$next = $nextNext;
}
$last->setNext(null);
$this->first = $tmp;
return true;
}
}

/**
* 打印现有的所有元素
*/
public function printLink() {
$tmp = $this->first;
if(is_null($tmp)) {
return false;
}
echo $tmp->getData();
while(!is_null($tmp->getNext())) {
$tmp = $tmp->getNext();
echo "->" . $tmp->getData();
}
echo "\n";
}
}

$link = new Link();
$link->addHead("1");
$link->printLink(); // 1

$link->addHead("5");
$link->printLink(); // 5->1

$link->addTail("9");
$link->printLink(); // 5->1->9

$link->addTail("7");
$link->printLink(); // 5->1->9->7

$link->add(3, "8");
$link->printLink(); // 5->1->9->8->7

print_r("链表长度:" . $link->getLength() . "\n");

$link->deleteFirst();
$link->printLink();

$link->deleteLast();
$link->printLink();

$link->deleteIndex(1);
$link->printLink();

print_r("链表长度:" . $link->getLength() . "\n");