Created
March 11, 2020 19:01
-
-
Save rajkumar-p/0dd8a81e3b7bdf3ab10ab17629ad977e to your computer and use it in GitHub Desktop.
Revisions
-
rajkumar-p created this gist
Mar 11, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ template<typename T> bool SortedList<T>::add(T elem) { NodeListWithLock<T> *prev = this->_head; prev->lock_node(); NodeListWithLock<T> *curr = prev->_next; while(curr != nullptr && curr->data() < elem) { curr->lock_node(); prev->unlock_node(); prev = curr; curr = curr->_next; } NodeListWithLock<T> *new_node = new NodeListWithLock<T>(elem); if (curr == nullptr) { prev->_next = new_node; prev->unlock_node(); } else { curr->lock_node(); prev->_next = new_node; new_node->_next = curr; prev->unlock_node(); curr->unlock_node(); } return true; }