| @@ 299-325 (lines=27) @@ | ||
| 296 | * @throws OutOfBoundsException if index is negative |
|
| 297 | * or is greater than the size of the list. |
|
| 298 | */ |
|
| 299 | public function delete($index) { |
|
| 300 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
| 301 | throw new OutOfBoundsException(); |
|
| 302 | } |
|
| 303 | ||
| 304 | // if the list is empty |
|
| 305 | if($this->head === null) { |
|
| 306 | return null; |
|
| 307 | } |
|
| 308 | ||
| 309 | // if only there is an element |
|
| 310 | if($this->head->next === $this->head) { |
|
| 311 | $temp = $this->head; |
|
| 312 | $this->head = null; |
|
| 313 | $this->size--; |
|
| 314 | ||
| 315 | return $temp->data; |
|
| 316 | } |
|
| 317 | ||
| 318 | if($index === 0) { |
|
| 319 | return $this->deleteBeginning(); |
|
| 320 | } else if($index === $this->size - 1) { |
|
| 321 | return $this->deleteEnd(); |
|
| 322 | } else { |
|
| 323 | return $this->deleteAt($index); |
|
| 324 | } |
|
| 325 | } |
|
| 326 | ||
| 327 | /** |
|
| 328 | * Deletes at the beginnig of the list and returns the data stored. |
|
| @@ 316-342 (lines=27) @@ | ||
| 313 | * @throws OutOfBoundsException if index is negative |
|
| 314 | * or is greater than the size of the list. |
|
| 315 | */ |
|
| 316 | public function delete($index) { |
|
| 317 | if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
|
| 318 | throw new OutOfBoundsException(); |
|
| 319 | } |
|
| 320 | ||
| 321 | // if the list is empty |
|
| 322 | if($this->head === null) { |
|
| 323 | return null; |
|
| 324 | } |
|
| 325 | ||
| 326 | // if only there is an element |
|
| 327 | if($this->head->next === $this->head) { |
|
| 328 | $temp = $this->head; |
|
| 329 | $this->head = null; |
|
| 330 | $this->size--; |
|
| 331 | ||
| 332 | return $temp->data; |
|
| 333 | } |
|
| 334 | ||
| 335 | if($index === 0) { |
|
| 336 | return $this->deleteBeginning(); |
|
| 337 | } else if($index === $this->size - 1) { |
|
| 338 | return $this->deleteEnd(); |
|
| 339 | } else { |
|
| 340 | return $this->deleteAt($index); |
|
| 341 | } |
|
| 342 | } |
|
| 343 | ||
| 344 | /** |
|
| 345 | * Deletes at the beginnig of the list and returns the data stored. |
|