Completed
Push — master ( 46289f...c51b27 )
by Siro Díaz
01:28
created

DoublyLinkedList::insertEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Lists;
10
11
use DataStructures\Lists\Traits\{CountTrait, ArrayAccessTrait};
12
use DataStructures\Lists\Nodes\DoublyLinkedListNode;
13
use DataStructures\Lists\ListAbstract;
14
use OutOfBoundsException;
15
16
/**
17
 * DoublyLinkedList
18
 *
19
 * DoublyLinkedList is a double linked list that has
20
 * a pointer to the next and the previous node.
21
 *
22
 * @author Siro Diaz Palazon <[email protected]>
23
 */
24
class DoublyLinkedList extends ListAbstract {
25
    use ArrayAccessTrait;
26
27
    private $head;
28
    private $tail;
29
    private $position;
30
    private $current;
31
32 View Code Duplication
    public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
        $this->head = null;
34
        $this->tail = &$this->head;
35
        $this->size = 0;
36
        $this->position = 0;
37
        $this->current = &$this->head;
38
    }
39
40
    /**
41
     * Removes all nodes of the list. It removes from the beginning.
42
     */
43
    public function clear() {
44
        while($this->head !== null) {
45
            $this->shift();
46
        }
47
    }
48
    
49
    /**
50
     * Gets the data stored in the position especified.
51
     *
52
     * @param integer $index Index that must be greater than 0
53
     *  and lower than the list size.
54
     * @return mixed The data stored in the given index
55
     * @throws OutOfBoundsException if index is out bounds.
56
     */
57 View Code Duplication
    public function get($index) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
        $node = $this->search($index);
59
        if($node === null) {
60
            return null;
61
        }
62
63
        return $node->data;
64
    }
65
66
    /**
67
     * Gets the node stored in the position especified.
68
     * If index is 0 or (size - 1) the method is O(1) else O(n).
69
     *
70
     * @param integer $index the position.
71
     * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1)
72
     * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null the node stored in $index.
73
     */
74
    protected function search($index) {
75
        if($index < 0 || $index > $this->size - 1) {
76
            throw new OutOfBoundsException();
77
        }
78
79
        if($index === 0) {
80
            return $this->head;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->head; of type null|DataStructures\List...es\DoublyLinkedListNode adds the type DataStructures\Lists\Nodes\DoublyLinkedListNode to the return on line 80 which is incompatible with the return type documented by DataStructures\Lists\DoublyLinkedList::search of type DataStructures\Lists\Dat...ublyLinkedListNode|null.
Loading history...
81
        }
82
83
        if($index === $this->size - 1) {
84
            return $this->tail;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->tail; of type null|DataStructures\List...es\DoublyLinkedListNode adds the type DataStructures\Lists\Nodes\DoublyLinkedListNode to the return on line 84 which is incompatible with the return type documented by DataStructures\Lists\DoublyLinkedList::search of type DataStructures\Lists\Dat...ublyLinkedListNode|null.
Loading history...
85
        }
86
87
        $current = &$this->head;
88
        if($index < (int) ceil($this->size / 2)) {
89
            $i = 0;
90
            while($i < $index) {
91
                $current = &$current->next;
92
                $i++;
93
            }    
94
        } else {
95
            $i = $this->size;
96
            while($i > $index) {
97
                $current = &$current->prev;
98
                $i--;
99
            }
100
        }
101
102
        return $current;
103
    }
104
105
    /**
106
     * Returns the data in the last node with O(1).
107
     *
108
     * @return mixed null if the list is empty.
109
     */
110
    public function getLast() {
111
        if($this->head === null) {
112
            return null;
113
        }
114
        return $this->tail->data;
115
    }
116
117
    /**
118
     * Returns the last node with O(1).
119
     *
120
     * @return DataStructures\Lists\Nodes\DoublyLinkedListNode|null if the list is empty.
121
     */
122
    public function searchLast() {
123
        if($this->head === null) {
124
            return null;
125
        }
126
        return $this->tail;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function contains($data) : bool {
133
        if($this->empty()) {
134
            return false;
135
        }
136
137
        $current = $this->head->next;
138
        $prev = $this->head;
139
        while($current !== $this->head) {
140
            if($prev->data === $data) {
141
                return true;
142
            }
143
            $prev = $current;
144
            $current = $current->next;
145
        }
146
147
        return false;
148
    }
149
150
    /**
151
     * {@inheritDoc}
152
     */
153 View Code Duplication
    public function indexOf($data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
        if($this->head === null) {
155
            return false;
156
        }
157
        
158
        $current = $this->head;
159
        $i = 0;
160
        
161
        while($i < $this->size) {
162
            if($current->data === $data) {
163
                return $i;
164
            }
165
166
            $current = $current->next;
167
            $i++;
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176 View Code Duplication
    public function lastIndexOf($data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
        if($this->head === null) {
178
            return false;
179
        }
180
        
181
        $current = $this->head;
182
        $i = 0;
183
        $pos = false;
184
        while($i < $this->size) {
185
            if($current->data == $data) {
186
                $pos = $i;
187
            }
188
189
            $current = $current->next;
190
            $i++;
191
        }
192
193
        return $pos;
194
    }
195
196
    /**
197
     * {@inheritDoc}
198
     */
199
    public function remove($data) {
200
        $current = &$this->head;
201
        $i = 0;
202
        
203
        if($this->head === null) {
204
            return null;
205
        }
206
207
        if($this->head->data === $data) {
208
            $this->head = &$this->head->next;
209
            $this->head->prev = &$this->tail;
210
            $this->size--;
211
            
212
            return $data;
213
        }
214
215 View Code Duplication
        while($i < $this->size) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
            if($current->data === $data) {
217
                $current->prev = &$current->next;
218
                $current = null;
219
                $this->size--;
220
                return $data;
221
            }
222
223
            $current = $current->next;
224
        }
225
226
        return null;
227
    }
228
    
229
    /**
230
     * Generator for retrieve all nodes stored.
231
     * 
232
     * @return null if the head is null (or list is empty)
233
     */
234 View Code Duplication
    public function getAll() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
        if($this->head === null) {
236
            return;
237
        }
238
239
        if($this->head === $this->tail) {
240
            yield $this->head->data;
241
        } else {
242
            $current = $this->head;
243
            $i = 0;
244
            while($i < $this->size) {
245
                yield $current->data;
246
                $current = $current->next;
247
                $i++;
248
            }
249
        }
250
    }
251
252
    /**
253
     * Inserts data in the specified position.
254
     *
255
     * @param integer $index the position.
256
     * @param mixed $data the data to store.
257
     */
258 View Code Duplication
    public function insert($index, $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
        if($index < 0) {
260
            throw new OutOfBoundsException();
261
        }
262
263
        if($index === 0) {
264
            $this->insertBeginning($data);
265
        } else if($index >= $this->size) {
266
            $this->insertEnd($data);
267
        } else if($index > 0 && $index < $this->size) {
268
            $this->insertAt($index, $data);
269
        }
270
        
271
        $this->size++;
272
    }
273
274
    /**
275
     * Inserts at the beginning of the list.
276
     *
277
     * @param mixed $data
278
     */
279
    private function insertBeginning($data) {
280
        $newNode = new DoublyLinkedListNode($data);
281
        if($this->head === null) {
282
            $newNode->next = &$this->head;
283
            $newNode->prev = &$this->head;
284
            $this->head = &$newNode;
285
            $this->tail = &$newNode;
286
        } else {
287
            $this->tail->next = &$newNode;
288
            $newNode->next = &$this->head;
289
            $newNode->prev = &$this->tail;
290
            $this->head = &$newNode;
291
        }
292
    }
293
294
    /**
295
     * Add a new node in the specified index.
296
     *
297
     * @param mixed $data the data to be stored.
298
     */
299
    private function insertEnd($data) {
300
        $newNode = new DoublyLinkedListNode($data);
301
        $this->tail->next = &$newNode;
302
        $newNode->next = &$this->head;
303
        $newNode->prev = &$this->tail;
304
        $this->tail = &$newNode;
305
        $this->head->prev = &$newNode;
306
    }
307
308
    /**
309
     * Add a new node in the specified index.
310
     *
311
     * @param integer $index the position.
312
     * @param mixed $data the data to be stored.
313
     */
314
    private function insertAt($index, $data) {
315
        $newNode = new DoublyLinkedListNode($data);
316
        $current = $this->head;
317
        $prev = null;
318
        $i = 0;
319
        while($i < $index) {
320
            $prev = $current;
321
            $current = $current->next;
322
            $i++;
323
        }
324
        
325
        $prev->next = &$newNode;
326
        $newNode->prev = &$prev;
327
        $newNode->next = &$current;
328
        $current->prev = &$newNode;
329
    }
330
    
331
    /**
332
     * Delete a node in the given position and returns it back.
333
     *
334
     * @param integer $index the position.
335
     * @throws OutOfBoundsException if index is negative
336
     *  or is greater than the size of the list.
337
     */
338 View Code Duplication
    public function delete($index) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
        if($index < 0 || ($index > 0 && $index > $this->size - 1)) {
340
            throw new OutOfBoundsException();
341
        }
342
343
        // if the list is empty
344
        if($this->head === null) {
345
            return null;
346
        }
347
        
348
        // if only there is an element
349
        if($this->head->next === $this->head) {
350
            $temp = $this->head;
351
            $this->head = null;
352
            $this->size--;
353
354
            return $temp->data;
355
        }
356
357
        if($index === 0) {
358
            return $this->deleteBeginning();
359
        } else if($index === $this->size - 1) {
360
            return $this->deleteEnd();
361
        } else {
362
            return $this->deleteAt($index);
363
        }
364
    }
365
366
    /**
367
     * Deletes at the beginnig of the list and returns the data stored.
368
     *
369
     * @return mixed the data stored in the node.
370
     */
371 View Code Duplication
    private function deleteBeginning() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
372
        $temp = $this->head;
373
        $this->head = &$this->head->next;
374
        $this->tail->next = &$this->head;
375
        $this->size--;
376
377
        return $temp->data;
378
    }
379
380
    /**
381
     * Deletes at the specified position and returns the data stored.
382
     *
383
     * @param integer $index the position.
384
     * @return mixed the data stored in the node.
385
     */
386
    private function deleteAt($index) {
387
        $i = 0;
388
        $prev = $this->head;
389
        $current = $this->head;
390
        
391
        while($i < $index) {
392
            $prev = $current;
393
            $current = $current->next;
394
            $i++;
395
        }
396
397
        $temp = $current;
398
        $prev->next = &$current->next;
399
        $current->next->prev = &$pre;
0 ignored issues
show
Bug introduced by
The variable $pre does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
400
        $current = null;
0 ignored issues
show
Unused Code introduced by
$current is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
401
        $this->size--;
402
403
        return $temp->data;
404
    }
405
406
    /**
407
     * Deletes at the end of the list and returns the data stored.
408
     *
409
     * @return mixed the data stored in the node.
410
     */
411 View Code Duplication
    private function deleteEnd() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
412
        $prev = $this->head;
413
        $current = $this->head;
414
        
415
        while($current !== $this->tail) {
416
            $prev = $current;
417
            $current = $current->next;
418
        }
419
        
420
        $temp = $current;
421
        $prev->next = &$this->head;
422
        $this->head->prev = &$prev;
423
        $this->tail = &$prev;
424
        $current = null;
0 ignored issues
show
Unused Code introduced by
$current is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
425
426
        $this->size--;
427
428
        return $temp->data;
429
    }
430
    
431
    /**
432
     * Converts/exports the list content into array type.
433
     *
434
     * @return array data stored in all nodes.
435
     */
436
    public function toArray() : array {
437
        $arr = [];
438
        foreach($this->getAll() as $node) {
439
            $arr[] = $node;
440
        }
441
442
        return $arr;
443
    }
444
445
    /**
446
     * Reset the cursor position.
447
     */
448
    public function rewind() {
449
        $this->position = 0;
450
        $this->current = &$this->head;
451
    }
452
453
    /**
454
     * Returns the current node data.
455
     *
456
     * @return mixed
457
     */
458
    public function current() {
459
        return $this->current->data;
460
    }
461
462
    /**
463
     * Key or index that indicates the cursor position.
464
     *
465
     * @return integer The current position.
466
     */
467
    public function key() {
468
        return $this->position;
469
    }
470
471
    /**
472
     * Move the cursor to the next node and increments the
473
     * position counter.
474
     */
475
    public function next() {
476
        ++$this->position;
477
        $this->current = $this->current->next;
478
    }
479
480
    /**
481
     * Returns if the current pointer position is valid.
482
     *
483
     * @return boolean true if pointer is not last, else false.
484
     */
485
    public function valid() {
486
        return $this->position < $this->size;
487
    }
488
}