DataStructures/Lists/CircularLinkedList.php 1 location
|
@@ 77-90 (lines=14) @@
|
| 74 |
|
* @param integer $index the position. |
| 75 |
|
* @param mixed $data the data to be stored. |
| 76 |
|
*/ |
| 77 |
|
protected function insertAt($index, $data) { |
| 78 |
|
$newNode = new SimpleLinkedListNode($data); |
| 79 |
|
$current = $this->head; |
| 80 |
|
$prev = null; |
| 81 |
|
$i = 0; |
| 82 |
|
while($i < $index) { |
| 83 |
|
$prev = $current; |
| 84 |
|
$current = $current->next; |
| 85 |
|
$i++; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
$prev->next = &$newNode; |
| 89 |
|
$newNode->next = &$current; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
/** |
| 93 |
|
* Returns the last node data with O(1). |
DataStructures/Lists/SimpleLinkedList.php 1 location
|
@@ 222-235 (lines=14) @@
|
| 219 |
|
* @param integer $index the position. |
| 220 |
|
* @param mixed $data the data to be stored. |
| 221 |
|
*/ |
| 222 |
|
protected function insertAt($index, $data) { |
| 223 |
|
$newNode = new SimpleLinkedListNode($data); |
| 224 |
|
$current = $this->head; |
| 225 |
|
$prev = null; |
| 226 |
|
$i = 0; |
| 227 |
|
while($i < $index) { |
| 228 |
|
$prev = $current; |
| 229 |
|
$current = $current->next; |
| 230 |
|
$i++; |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
$prev->next = &$newNode; |
| 234 |
|
$newNode->next = &$current; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
protected function insertEnd($data) { |
| 238 |
|
$newNode = new SimpleLinkedListNode($data); |