DataStructures/Lists/CircularLinkedList.php 1 location
|
@@ 98-111 (lines=14) @@
|
| 95 |
|
* @param integer $index the position. |
| 96 |
|
* @param mixed $data the data to be stored. |
| 97 |
|
*/ |
| 98 |
|
private function insertAt($index, $data) { |
| 99 |
|
$newNode = new SimpleLinkedListNode($data); |
| 100 |
|
$current = $this->head; |
| 101 |
|
$prev = null; |
| 102 |
|
$i = 0; |
| 103 |
|
while($i < $index) { |
| 104 |
|
$prev = $current; |
| 105 |
|
$current = $current->next; |
| 106 |
|
$i++; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
$prev->next = &$newNode; |
| 110 |
|
$newNode->next = &$current; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
/** |
| 114 |
|
* Returns the last node data with O(1). |
DataStructures/Lists/SimpleLinkedList.php 1 location
|
@@ 220-233 (lines=14) @@
|
| 217 |
|
* @param integer $index the position. |
| 218 |
|
* @param mixed $data the data to be stored. |
| 219 |
|
*/ |
| 220 |
|
protected function insertAt($index, $data) { |
| 221 |
|
$newNode = new SimpleLinkedListNode($data); |
| 222 |
|
$current = $this->head; |
| 223 |
|
$prev = null; |
| 224 |
|
$i = 0; |
| 225 |
|
while($i < $index) { |
| 226 |
|
$prev = $current; |
| 227 |
|
$current = $current->next; |
| 228 |
|
$i++; |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
$prev->next = &$newNode; |
| 232 |
|
$newNode->next = &$current; |
| 233 |
|
} |
| 234 |
|
|
| 235 |
|
protected function insertEnd($data) { |
| 236 |
|
$newNode = new SimpleLinkedListNode($data); |