Skip to content

Instantly share code, notes, and snippets.

@rajkumar-p
Created March 2, 2020 18:11
Show Gist options
  • Save rajkumar-p/528e7cc7a8cf0a11decccd9be7103630 to your computer and use it in GitHub Desktop.
Save rajkumar-p/528e7cc7a8cf0a11decccd9be7103630 to your computer and use it in GitHub Desktop.
Remove an element from the concurrent list
template<typename T>
bool List<T>::remove_if_exists(T elem)
{
// Empty list
if (this->empty()) {
return false;
}
// Need to remove the head element
if (this->_head->data() == elem) {
NodeList<T> *node = this->_head;
this->_head = this->_head->_next;
delete node;
return true;
}
// Others
NodeList<T> *prev = this->_head;
NodeList<T> *node = prev->_next;
while (node != nullptr) {
if (node->data() == elem) {
prev->_next = node->_next;
node->_next = nullptr;
delete node;
return true;
}
prev = node;
node = node->_next;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment