diff --git a/DataStructures/src/linkedlist.h b/DataStructures/src/linkedlist.h index e58cf39..bf95478 100644 --- a/DataStructures/src/linkedlist.h +++ b/DataStructures/src/linkedlist.h @@ -14,6 +14,11 @@ namespace Datastructures { bool insert(T value, int index); bool remove(int index); int count() const; + int find(const T &value) const; + + enum EFindResult { + eNotFound = -1, + }; private: std::shared_ptr> getIndex(int index); @@ -96,6 +101,19 @@ namespace Datastructures { return true; } + template + int LinkedList::find(const T& value) const { + //Start at the head of the list + std::shared_ptr> node = mHead; + for (int i = 0; i < this->mCount; i++) + {//Loop through list to try and find value + if (node->value == value) + return i; + node = node->next; + } + return EFindResult::eNotFound; //-1 + } + template std::shared_ptr> LinkedList::getIndex(int index) { //Check if the value lies within the range of the list. @@ -103,7 +121,7 @@ namespace Datastructures { return nullptr; std::shared_ptr> node = mHead; - for (size_t i = 0; i < index; i++) + for (int i = 0; i < index; i++) {//Interate through the linked list i times to get to the index. node = node->next; } diff --git a/src/main.cpp b/src/main.cpp index 8ea34ac..d4ed7d3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,4 +6,5 @@ int main() { list.append(200); list.insert(20, 2); list.remove(0); + int x = list.find(10); } \ No newline at end of file