@@ 78-91 (lines=14) @@ | ||
75 | * @param integer $index the position. |
|
76 | * @param mixed $data the data to be stored. |
|
77 | */ |
|
78 | protected function insertAt($index, $data) { |
|
79 | $newNode = new SinglyLinkedListNode($data); |
|
80 | $current = $this->head; |
|
81 | $prev = null; |
|
82 | $i = 0; |
|
83 | while($i < $index) { |
|
84 | $prev = $current; |
|
85 | $current = $current->next; |
|
86 | $i++; |
|
87 | } |
|
88 | ||
89 | $prev->next = &$newNode; |
|
90 | $newNode->next = &$current; |
|
91 | } |
|
92 | ||
93 | /** |
|
94 | * Returns the last node with O(1). |
@@ 194-207 (lines=14) @@ | ||
191 | * @param integer $index the position. |
|
192 | * @param mixed $data the data to be stored. |
|
193 | */ |
|
194 | protected function insertAt($index, $data) { |
|
195 | $newNode = new SinglyLinkedListNode($data); |
|
196 | $current = $this->head; |
|
197 | $prev = null; |
|
198 | $i = 0; |
|
199 | while($i < $index) { |
|
200 | $prev = $current; |
|
201 | $current = $current->next; |
|
202 | $i++; |
|
203 | } |
|
204 | ||
205 | $prev->next = &$newNode; |
|
206 | $newNode->next = &$current; |
|
207 | } |
|
208 | ||
209 | protected function insertEnd($data) { |
|
210 | $newNode = new SinglyLinkedListNode($data); |