| @@ 187-208 (lines=22) @@ | ||
| 184 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
|
| 185 | * @return mixed the data stored in $index node. |
|
| 186 | */ |
|
| 187 | public function get($index) { |
|
| 188 | if($index < 0 || $index > $this->size - 1) { |
|
| 189 | throw new OutOfBoundsException(); |
|
| 190 | } |
|
| 191 | ||
| 192 | if($index === 0) { |
|
| 193 | return $this->head->data; |
|
| 194 | } |
|
| 195 | ||
| 196 | if($index === $this->size - 1) { |
|
| 197 | return $this->getLast(); |
|
| 198 | } |
|
| 199 | ||
| 200 | $i = 0; |
|
| 201 | $current = $this->head; |
|
| 202 | while($i < $index) { |
|
| 203 | $current = $current->next; |
|
| 204 | $i++; |
|
| 205 | } |
|
| 206 | ||
| 207 | return $current->data; |
|
| 208 | } |
|
| 209 | ||
| 210 | /** |
|
| 211 | * Returns the node stored in the given position. |
|
| @@ 218-239 (lines=22) @@ | ||
| 215 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
|
| 216 | * @return DataStructures\Lists\Nodes\SimpleLinkedListNode|null the node stored in $index. |
|
| 217 | */ |
|
| 218 | protected function search($index) { |
|
| 219 | if($index < 0 || $index > $this->size - 1) { |
|
| 220 | throw new OutOfBoundsException(); |
|
| 221 | } |
|
| 222 | ||
| 223 | if($index === 0) { |
|
| 224 | return $this->head; |
|
| 225 | } |
|
| 226 | ||
| 227 | if($index === $this->size - 1) { |
|
| 228 | return $this->getLast(); |
|
| 229 | } |
|
| 230 | ||
| 231 | $i = 0; |
|
| 232 | $current = $this->head; |
|
| 233 | while($i < $index) { |
|
| 234 | $current = $current->next; |
|
| 235 | $i++; |
|
| 236 | } |
|
| 237 | ||
| 238 | return $current; |
|
| 239 | } |
|
| 240 | ||
| 241 | /** |
|
| 242 | * Generator for retrieve all nodes stored. |
|