@@ 340-366 (lines=27) @@ | ||
337 | * @throws OutOfBoundsException if index is negative |
|
338 | * or is greater than the size of the list. |
|
339 | */ |
|
340 | public function delete($index) { |
|
341 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
342 | throw new OutOfBoundsException(); |
|
343 | } |
|
344 | ||
345 | // if the list is empty |
|
346 | if($this->head === null) { |
|
347 | return null; |
|
348 | } |
|
349 | ||
350 | // if only there is an element |
|
351 | if($this->head->next === $this->head) { |
|
352 | $temp = $this->head; |
|
353 | $this->head = null; |
|
354 | $this->size--; |
|
355 | ||
356 | return $temp->data; |
|
357 | } |
|
358 | ||
359 | if($index === 0) { |
|
360 | return $this->deleteBeginning(); |
|
361 | } else if($index === $this->size - 1) { |
|
362 | return $this->deleteEnd(); |
|
363 | } else { |
|
364 | return $this->deleteAt($index); |
|
365 | } |
|
366 | } |
|
367 | ||
368 | /** |
|
369 | * Deletes at the beginnig of the list and returns the data stored. |
@@ 358-384 (lines=27) @@ | ||
355 | * @throws OutOfBoundsException if index is negative |
|
356 | * or is greater than the size of the list. |
|
357 | */ |
|
358 | public function delete($index) { |
|
359 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
360 | throw new OutOfBoundsException(); |
|
361 | } |
|
362 | ||
363 | // if the list is empty |
|
364 | if($this->head === null) { |
|
365 | return null; |
|
366 | } |
|
367 | ||
368 | // if only there is an element |
|
369 | if($this->head->next === $this->head) { |
|
370 | $temp = $this->head; |
|
371 | $this->head = null; |
|
372 | $this->size--; |
|
373 | ||
374 | return $temp->data; |
|
375 | } |
|
376 | ||
377 | if($index === 0) { |
|
378 | return $this->deleteBeginning(); |
|
379 | } else if($index === $this->size - 1) { |
|
380 | return $this->deleteEnd(); |
|
381 | } else { |
|
382 | return $this->deleteAt($index); |
|
383 | } |
|
384 | } |
|
385 | ||
386 | /** |
|
387 | * Deletes at the beginnig of the list and returns the data stored. |