May 2017
Intermediate to advanced
340 pages
8h 16m
English
Inserting after a specific node is similar to the method we just discussed. Here, we need to change the next and previous nodes for the new node, the target node, and the node following the target node. Here is the code for that:
public function insertAfter(string $data = NULL, string $query = NULL) { $newNode = new ListNode($data); if ($this->_firstNode) { $nextNode = NULL; $currentNode = $this->_firstNode; while ($currentNode !== NULL) { if ($currentNode->data === $query) { if ($nextNode !== NULL) { $newNode->next = $nextNode; } if ($currentNode === $this->_lastNode) { $this->_lastNode = $newNode; } $currentNode->next = $newNode; $nextNode->prev = $newNode; $newNode->prev = $currentNode; ...Read now
Unlock full access