-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathDoublyLinkedList.php
379 lines (309 loc) · 10.7 KB
/
DoublyLinkedList.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
require_once __DIR__ . '/Node.php';
/**
* Doubly Linked List
*/
class DoublyLinkedList
{
public ?Node $head = null;
public ?Node $tail = null;
// Constructor
public function __construct()
{
$this->head = null;
$this->tail = null;
}
// Destructor
public function __destruct()
{
$this->head = null;
$this->tail = null;
}
// Append to the end of the list
public function append($data): void
{
$newNode = new Node($data);
// If the list is empty, set the head and tail to the new node
if ($this->head === null) {
$this->head = $newNode;
$this->tail = $newNode;
return;
}
// Otherwise, set the tail's next node to the new node
$this->tail->next = $newNode;
// Set the new node's previous node to the tail
$newNode->prev = $this->tail;
// Set the tail to the new node
$this->tail = $newNode;
}
// Insert a node after a given position
public function insert($data, $position): void
{
$newNode = new Node($data);
// If the list is empty, set the head and tail to the new node
if ($this->head === null) {
$this->head = $newNode;
$this->tail = $newNode;
return;
}
// If the position is 0, set the new node's next node to the head
// Set the head's previous node to the new node
// Set the head to the new node
if ($position === 0) {
$newNode->next = $this->head;
$this->head->prev = $newNode;
$this->head = $newNode;
return;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Loop through the list until we reach the position
for ($i = 0; $i < $position; $i++) {
// If the current node is null, we've reached the end of the list
// Set the tail's next node to the new node
// Set the new node's previous node to the tail
// Set the tail to the new node
if ($current === null) {
$this->tail->next = $newNode;
$newNode->prev = $this->tail;
$this->tail = $newNode;
return;
}
// Otherwise, set the current node to the next node
$current = $current->next;
}
// Set the new node's next node to the current node
// Set the new node's previous node to the current node's previous node
// Set the current node's previous node's next node to the new node
// Set the current node's previous node to the new node
$newNode->next = $current;
$newNode->prev = $current->prev;
$current->prev->next = $newNode;
$current->prev = $newNode;
}
// Delete a node from the list
public function delete($data): void
{
// If the list is empty, return
if ($this->head === null) {
return;
}
// If the head's data is the data we're looking for
// Set the head to the head's next node
// Set the head's previous node to null
if ($this->head->data === $data) {
$this->head = $this->head->next;
$this->head->prev = null;
return;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Loop through the list until we reach the end of the list
while ($current !== null) {
// If the current node's data is the data we're looking for
// Set the current node's previous node's next node to the current node's next node
// Set the current node's next node's previous node to the current node's previous node
if ($current->data === $data) {
$current->prev->next = $current->next;
$current->next->prev = $current->prev;
return;
}
// Otherwise, set the current node to the next node
$current = $current->next;
}
}
// Delete a node from a given position
public function deleteAt($position): void
{
// If the list is empty, return
if ($this->head === null) {
return;
}
// If the position is 0
// Set the head to the head's next node
// Set the head's previous node to null
if ($position === 0) {
$this->head = $this->head->next;
$this->head->prev = null;
return;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Loop through the list until we reach the position
for ($i = 0; $i < $position; $i++) {
// If the current node is null, we've reached the end of the list
// Set the tail to the current node's previous node
// Set the tail's next node to null
if ($current === null) {
$this->tail = $current->prev;
$this->tail->next = null;
return;
}
// Otherwise, set the current node to the next node
$current = $current->next;
}
// Set the current node's previous node's next node to the current node's next node
// Set the current node's next node's previous node to the current node's previous node
$current->prev->next = $current->next;
$current->next->prev = $current->prev;
}
// Print the list
public function printList(): void
{
// If the list is empty, return
if ($this->head === null) {
return;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Loop through the list until we reach the end of the list
while ($current !== null) {
// Print the current node's data
echo $current->data . "\n";
// Set the current node to the next node
$current = $current->next;
}
}
// Print the list in reverse
public function printListReverse(): void
{
// If the list is empty, return
if ($this->head === null) {
return;
}
// Otherwise, set the current node to the tail
$current = $this->tail;
// Loop through the list until we reach the beginning of the list
while ($current !== null) {
// Print the current node's data
echo $current->data . "\n";
// Set the current node to the previous node
$current = $current->prev;
}
}
// Reverse the list
public function reverse(): void
{
// If the list is empty, return
if ($this->head === null) {
return;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Loop through the list until we reach the end of the list
while ($current !== null) {
// Set the temp node to the current node's next node
$temp = $current->next;
// Set the current node's next node to the current node's previous node
$current->next = $current->prev;
// Set the current node's previous node to the temp node
$current->prev = $temp;
// Set the current node to the temp node
$current = $temp;
}
// Set the temp node to the head
$temp = $this->head;
// Set the head to the tail
$this->head = $this->tail;
// Set the tail to the temp node
$this->tail = $temp;
}
// Get the length of the list
public function length(): int
{
// If the list is empty, return 0
if ($this->head === null) {
return 0;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Set the length to 0
$length = 0;
// Loop through the list until we reach the end of the list
while ($current !== null) {
// Increment the length
$length++;
// Set the current node to the next node
$current = $current->next;
}
// Return the length
return $length;
}
// Search for a node
public function search($data): ?Node
{
// If the list is empty, return null
if ($this->head === null) {
return null;
}
// Otherwise, set the current node to the head
$current = $this->head;
// Loop through the list until we reach the end of the list
while ($current !== null) {
// If the current node's data is the data we're looking for, return the current node
if ($current->data === $data) {
return $current;
}
// Set the current node to the next node
$current = $current->next;
}
// Return null
return null;
}
// Is the list empty?
public function isEmpty(): bool
{
// If the head is null, return true
if ($this->head === null) {
return true;
}
// Otherwise, return false
return false;
}
// To String
public function __toString(): string
{
// If the list is empty, return an empty string
if ($this->head === null) {
return '';
}
// Otherwise, set the current node to the head
$current = $this->head;
// Set the string to an empty string
$string = '';
// Loop through the list until we reach the end of the list
while ($current !== null) {
// Append the current node's data to the string
$string .= $current->data;
// If the current node's next node is not null, append a comma and a space to the string
if ($current->next !== null) {
$string .= ', ';
}
// Set the current node to the next node
$current = $current->next;
}
// Return the string
return $string;
}
// To Array
public function toArray(): array
{
// If the list is empty, return an empty array
if ($this->head === null) {
return [];
}
// Otherwise, set the current node to the head
$current = $this->head;
// Set the array to an empty array
$array = [];
// Loop through the list until we reach the end of the list
while ($current !== null) {
// Append the current node's data to the array
$array[] = $current->data;
// Set the current node to the next node
$current = $current->next;
}
// Return the array
return $array;
}
}