@@ 252-278 (lines=27) @@ | ||
249 | * @throws OutOfBoundsException if index is negative |
|
250 | * or is greater than the size of the list. |
|
251 | */ |
|
252 | public function delete($index) { |
|
253 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
254 | throw new OutOfBoundsException(); |
|
255 | } |
|
256 | ||
257 | // if the list is empty |
|
258 | if($this->head === null) { |
|
259 | return null; |
|
260 | } |
|
261 | ||
262 | // if only there is an element |
|
263 | if($this->head->next === $this->head) { |
|
264 | $temp = $this->head; |
|
265 | $this->head = null; |
|
266 | $this->size--; |
|
267 | ||
268 | return $temp->data; |
|
269 | } |
|
270 | ||
271 | if($index === 0) { |
|
272 | return $this->deleteBeginning(); |
|
273 | } else if($index === $this->size - 1) { |
|
274 | return $this->deleteEnd(); |
|
275 | } else { |
|
276 | return $this->deleteAt($index); |
|
277 | } |
|
278 | } |
|
279 | ||
280 | /** |
|
281 | * Deletes at the beginnig of the list and returns the data stored. |
@@ 282-308 (lines=27) @@ | ||
279 | * @throws OutOfBoundsException if index is negative |
|
280 | * or is greater than the size of the list. |
|
281 | */ |
|
282 | public function delete($index) { |
|
283 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
284 | throw new OutOfBoundsException(); |
|
285 | } |
|
286 | ||
287 | // if the list is empty |
|
288 | if($this->head === null) { |
|
289 | return null; |
|
290 | } |
|
291 | ||
292 | // if only there is an element |
|
293 | if($this->head->next === $this->head) { |
|
294 | $temp = $this->head; |
|
295 | $this->head = null; |
|
296 | $this->size--; |
|
297 | ||
298 | return $temp->data; |
|
299 | } |
|
300 | ||
301 | if($index === 0) { |
|
302 | return $this->deleteBeginning(); |
|
303 | } else if($index === $this->size - 1) { |
|
304 | return $this->deleteEnd(); |
|
305 | } else { |
|
306 | return $this->deleteAt($index); |
|
307 | } |
|
308 | } |
|
309 | ||
310 | /** |
|
311 | * Deletes at the beginnig of the list and returns the data stored. |