Created
March 2, 2020 18:11
-
-
Save rajkumar-p/528e7cc7a8cf0a11decccd9be7103630 to your computer and use it in GitHub Desktop.
Remove an element from the concurrent list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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