Completed
Push — master ( 555aef...4f8fd0 )
by Siro Díaz
01:26
created

SimpleLinkedList::deleteBeginning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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\SimpleLinkedListNode;
13
use OutOfBoundsException;
14
use Iterator;
15
16
/**
17
 * SimpleLinkedList
18
 *
19
 * SimpleLinkedList is a singly linked list that has
20
 * a pointer to the next node but last node points to null.
21
 *
22
 * @author Siro Diaz Palazon <[email protected]>
23
 */
24
class SimpleLinkedList extends ListAbstract {
25
    use ArrayAccessTrait;
26
    
27
    protected $head;
28
    private $position;
29
    private $current;
30
31 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...
32
        $this->head = null;
33
        $this->size = 0;
34
        $this->position = 0;
35
        $this->current = &$this->head;
36
    }
37
38
    /**
39
     * Gets the data stored in the position especified.
40
     *
41
     * @param integer $index Index that must be greater than 0
42
     *  and lower than the list size.
43
     * @return mixed The data stored in the given index
44
     * @throws OutOfBoundsException if index is out bounds.
45
     */
46 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...
47
        $node = $this->search($index);
48
        if($node === null) {
49
            return null;
50
        }
51
52
        return $node->data;
53
    }
54
55
    /**
56
     * Returns the node stored in the given position.
57
     *
58
     * @param integer $index the position.
59
     * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1)
60
     * @return DataStructures\Lists\Nodes\SimpleLinkedListNode|null the node stored in $index.
61
     */
62
    protected function search($index) {
63
        if($index > $this->size - 1 || $index < 0) {
64
            throw new OutOfBoundsException();
65
        }
66
67
        $current = $this->head;
68
        $i = 0;
69 View Code Duplication
        while($i < $index && $current->next !== null) {
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...
70
            $current = $current->next;
71
            $i++;
72
        }
73
74
        return $current;
75
    }
76
77
    /**
78
     * Generator for retrieve all nodes stored.
79
     * 
80
     * @return null if the head is null (or list is empty)
81
     */
82
    public function getAll() {
83
        if($this->head === null) {
84
            return;
85
        }
86
87
        $current = $this->head;
88
        while($current !== null) {
89
            yield $current->data;
90
            $current = $current->next;
91
        }
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function contains($data) : bool {
98
        if($this->empty()) {
99
            return false;
100
        }
101
102
        $current = $this->head;
103
        while($current !== null) {
104
            if($current->data === $data) {
105
                return true;
106
            }
107
            $current = $current->next;
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 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...
117
        if($this->head === null) {
118
            return false;
119
        }
120
        
121
        $current = $this->head;
122
        $i = 0;
123
        
124
        while($i < $this->size) {
125
            if($current->data === $data) {
126
                return $i;
127
            }
128
129
            $current = $current->next;
130
            $i++;
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 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...
140
        if($this->head === null) {
141
            return false;
142
        }
143
        
144
        $current = $this->head;
145
        $i = 0;
146
        $pos = false;
147
        while($i < $this->size) {
148
            if($current->data == $data) {
149
                $pos = $i;
150
            }
151
152
            $current = $current->next;
153
            $i++;
154
        }
155
156
        return $pos;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162
    public function remove($data) {
163
        $current = &$this->head;
164
        $prev = null;
165
        $i = 0;
166
        
167
        if($this->head === null) {
168
            return null;
169
        }
170
171
        if($this->head->data === $data) {
172
            $this->head = &$this->head->next;
173
            $this->size--;
174
            return $data;
175
        }
176
        
177 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...
178
            if($current->data === $data) {
179
                $prev->next = &$current->next;
180
                $this->size--;
181
182
                return $data;
183
            }
184
185
            $prev = $current;
186
            $current = $current->next;
187
        }
188
189
        return null;
190
    }
191
192
    /**
193
     * Add a new node in the specified index.
194
     *
195
     * @param integer $index the position.
196
     * @param mixed $data the data to be stored.
197
     */
198 View Code Duplication
    protected function insertAt($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...
199
        $newNode = new SimpleLinkedListNode($data);
200
        $current = $this->head;
201
        $prev = null;
202
        $i = 0;
203
        while($i < $index) {
204
            $prev = $current;
205
            $current = $current->next;
206
            $i++;
207
        }
208
        
209
        $prev->next = &$newNode;
210
        $newNode->next = &$current;
211
    }
212
213
    protected function insertEnd($data) {
214
        $newNode = new SimpleLinkedListNode($data);
215 View Code Duplication
        if($this->head === null) {
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
            $this->head = &$newNode;
217
            $this->current = &$this->head;
218
        } else {
219
            $current = $this->head;
220
            while($current->next !== null) {
221
                $current = $current->next;
222
            }
223
            $current->next = &$newNode;
224
        }
225
    }
226
227
    /**
228
     * {@inheritDoc}
229
     */
230 View Code Duplication
    protected function insertBeginning($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...
231
        $newNode = new SimpleLinkedListNode($data);
232
        if($this->head === null) {
233
            $this->head = &$newNode;
234
        } else {
235
            $newNode->next = $this->head;
236
            $this->head = &$newNode;
237
        }
238
    }
239
240
    /**
241
     * Delete a node in the given position and returns it back.
242
     *
243
     * @param integer $index the position.
244
     * @throws OutOfBoundsException if index is negative
245
     *  or is greater than the size of the list.
246
     */
247
    public function delete($index) {
248
        if($index < 0) {
249
            throw new OutOfBoundsException();
250
        }
251
        if($this->head === null) {
252
            return null;
253
        }
254
255
        if($index >= $this->size) {
256
            return null;    // It should return an exception
257
        }
258
259
        if($index === 0) {
260
            $node = $this->head;
261
            $this->head = $this->head->next;
262
            $this->current = &$this->head;
263
            $this->size--;
264
            return $node->data;
265
        }
266
        
267
        $i = 0;
268
        $current = $this->head;
269
        $prev = $current;
270 View Code Duplication
        while($i < $index && $current->next !== null) {
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...
271
            $prev = $current;
272
            $current = $current->next;
273
        }
274
275
        if($index === $this->size - 1) {
276
            $prev->next = null;
277
            $this->size--;
278
            return $current->data;
279
        } else {
280
            $prev->next = $current->next;
281
        }
282
        $this->size--;
283
284
        return $prev->data;
285
    }
286
287
    protected function deleteBeginning() {}
288
289
    protected function deleteAt($index) {}
290
291
    protected function deleteEnd() {}
292
293
    /**
294
     * Converts/exports the list content into array type.
295
     *
296
     * @return array data stored in all nodes.
297
     */
298
    public function toArray() : array {
299
        $arr = [];
300
        foreach($this->getAll() as $node) {
301
            $arr[] = $node;
302
        }
303
304
        return $arr;
305
    }
306
307
    /**
308
     * Reset the cursor position.
309
     */
310
    public function rewind() {
311
        $this->position = 0;
312
        $this->current = $this->head;
313
    }
314
315
    /**
316
     * Returns the current node data.
317
     *
318
     * @return mixed
319
     */
320
    public function current() {
321
        return $this->current->data;
322
    }
323
324
    /**
325
     * Key or index that indicates the cursor position.
326
     *
327
     * @return integer The current position.
328
     */
329
    public function key() {
330
        return $this->position;
331
    }
332
333
    /**
334
     * Move the cursor to the next node and increments the
335
     * position counter.
336
     */
337
    public function next() {
338
        ++$this->position;
339
        $this->current = $this->current->next;
340
    }
341
342
    /**
343
     * Returns if the current pointer position is valid.
344
     *
345
     * @return boolean true if pointer is not last, else false.
346
     */
347
    public function valid() {
348
        return $this->current !== null;
349
    }
350
}