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