@@ 320-346 (lines=27) @@ | ||
317 | * @throws OutOfBoundsException if index is negative |
|
318 | * or is greater than the size of the list. |
|
319 | */ |
|
320 | public function delete($index) { |
|
321 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
322 | throw new OutOfBoundsException(); |
|
323 | } |
|
324 | ||
325 | // if the list is empty |
|
326 | if($this->head === null) { |
|
327 | return null; |
|
328 | } |
|
329 | ||
330 | // if only there is an element |
|
331 | if($this->head->next === $this->head) { |
|
332 | $temp = $this->head; |
|
333 | $this->head = null; |
|
334 | $this->size--; |
|
335 | ||
336 | return $temp->data; |
|
337 | } |
|
338 | ||
339 | if($index === 0) { |
|
340 | return $this->deleteBeginning(); |
|
341 | } else if($index === $this->size - 1) { |
|
342 | return $this->deleteEnd(); |
|
343 | } else { |
|
344 | return $this->deleteAt($index); |
|
345 | } |
|
346 | } |
|
347 | ||
348 | /** |
|
349 | * Deletes at the beginnig of the list and returns the data stored. |
@@ 338-364 (lines=27) @@ | ||
335 | * @throws OutOfBoundsException if index is negative |
|
336 | * or is greater than the size of the list. |
|
337 | */ |
|
338 | public function delete($index) { |
|
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. |