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