| @@ 168-189 (lines=22) @@ | ||
| 165 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
|
| 166 | * @return mixed the data stored in $index node. |
|
| 167 | */ |
|
| 168 | public function get($index) { |
|
| 169 | if($index < 0 || $index > $this->size - 1) { |
|
| 170 | throw new OutOfBoundsException(); |
|
| 171 | } |
|
| 172 | ||
| 173 | if($index === 0) { |
|
| 174 | return $this->head->data; |
|
| 175 | } |
|
| 176 | ||
| 177 | if($index === $this->size - 1) { |
|
| 178 | return $this->getLast(); |
|
| 179 | } |
|
| 180 | ||
| 181 | $i = 0; |
|
| 182 | $current = $this->head; |
|
| 183 | while($i < $index) { |
|
| 184 | $current = $current->next; |
|
| 185 | $i++; |
|
| 186 | } |
|
| 187 | ||
| 188 | return $current->data; |
|
| 189 | } |
|
| 190 | ||
| 191 | /** |
|
| 192 | * Returns the node stored in the given position. |
|
| @@ 199-220 (lines=22) @@ | ||
| 196 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
|
| 197 | * @return DataStructures\Lists\Nodes\SimpleLinkedListNode|null the node stored in $index. |
|
| 198 | */ |
|
| 199 | protected function search($index) { |
|
| 200 | if($index < 0 || $index > $this->size - 1) { |
|
| 201 | throw new OutOfBoundsException(); |
|
| 202 | } |
|
| 203 | ||
| 204 | if($index === 0) { |
|
| 205 | return $this->head; |
|
| 206 | } |
|
| 207 | ||
| 208 | if($index === $this->size - 1) { |
|
| 209 | return $this->getLast(); |
|
| 210 | } |
|
| 211 | ||
| 212 | $i = 0; |
|
| 213 | $current = $this->head; |
|
| 214 | while($i < $index) { |
|
| 215 | $current = $current->next; |
|
| 216 | $i++; |
|
| 217 | } |
|
| 218 | ||
| 219 | return $current; |
|
| 220 | } |
|
| 221 | ||
| 222 | /** |
|
| 223 | * Generator for retrieve all nodes stored. |
|