May 2017
Intermediate to advanced
340 pages
8h 16m
English
When we are deleting a node from the middle of the list, we have to readjust the previous and the following node of the item we are looking for. First, we will find the intended node. Get the previous node of the target node, along with the next node. Then, assign the node following the previous node to point to the next node after the target node, and the same applies for the previous node in a reverse manner. Here is the code for that:
public function delete(string $query = NULL) { if ($this->_firstNode) { $previous = NULL; $currentNode = $this->_firstNode; while ($currentNode !== NULL) { if ($currentNode->data === $query) { if ($currentNode->next === NULL) { $previous->next = NULL; } else { ...Read now
Unlock full access