Searching for and deleting a node

We can delete any node from our list using a search and delete operation. First, we search for the node from the list and then remove the node by removing references from the node. Here is the code to achieve this:

    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 {
$previous->next = $currentNode->next;
}

$this->_totalNode--;
break;
}
$previous = $currentNode;
$currentNode = $currentNode->next;
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset