diff --git a/src/classes/list/doubly_linked_list.h b/src/classes/list/doubly_linked_list.h index b59e6c64..1980d562 100644 --- a/src/classes/list/doubly_linked_list.h +++ b/src/classes/list/doubly_linked_list.h @@ -164,27 +164,24 @@ template bool doubly_linked_list::search(T key) { return false; } else { std::shared_ptr t = root; - while (t != tail && t->val != key) { + while (t != nullptr && t->val != key) { t = t->next; } - if (t == tail || t == nullptr) { - return false; - } - return true; + return (t == nullptr || t->val != key) ? false : true; } return false; } template void doubly_linked_list::push_back(T key) { std::shared_ptr p = std::make_shared(key); - if (root == nullptr) { - root = p; - } + p->prev = tail; + p->next = nullptr; if (tail != nullptr) { tail->next = p; } - p->next = nullptr; - p->prev = tail; + if (root == nullptr) { + root = p; + } tail = p; _size++; } @@ -196,6 +193,9 @@ template void doubly_linked_list::push_front(T key) { if (root != nullptr) { root->prev = p; } + if(tail == nullptr) { + tail = p; + } root = p; _size++; } @@ -204,9 +204,6 @@ template void doubly_linked_list::erase(T key) { if (root == nullptr) { return; } - if (root->val == key) { - root = root->next; - } std::shared_ptr head = root; while (head && head->val != key) { head = head->next; @@ -215,9 +212,15 @@ template void doubly_linked_list::erase(T key) { return; } if (head->next != nullptr) { + if(head == root) { + root = root -> next; + } head->next->prev = head->prev; } if (head->prev != nullptr) { + if(head == tail) { + tail = tail->prev; + } head->prev->next = head->next; } } diff --git a/tests/list/doubly_list.cc b/tests/list/doubly_list.cc index a19e85e5..f49fcf08 100644 --- a/tests/list/doubly_list.cc +++ b/tests/list/doubly_list.cc @@ -131,6 +131,63 @@ TEST_CASE("testing operator = in doubly list") { REQUIRE(v2 == v); } +TEST_CASE("Testing searching for issue #88") { + // Bug in search method + doubly_linked_list l({1, 2, 3, 4}); + REQUIRE(l.search(4) == true); + doubly_linked_list ll({'a', 'b', 'c', 'd', 'e'}); + REQUIRE(ll.search('e') == true); + ll.erase('e'); + REQUIRE(ll.search('e') == false); + REQUIRE(ll.search('d') == true); + ll.erase('d'); + REQUIRE(ll.search('d') == false); + + // Bug in push_front method + doubly_linked_list l2; + l2.push_front(1); + l2.push_back(2); + l2.push_back(3); + std::vector check = {1, 2, 3}; + int idx = 0; + for(auto it = l2.begin(); it != l2.end(); it++) { + REQUIRE(*(it) == check[idx++]); + } + + // Bug in erase method + doubly_linked_list l3({1, 2, 3}); + l3.erase(3); + l3.push_back(4); + l3.push_back(5); + check.clear(); + check = {1, 2, 4, 5}; + idx = 0; + for(auto it = l3.begin(); it != l3.end(); it++) { + REQUIRE(*(it) == check[idx++]); + } + + doubly_linked_list l4({1, 2, 3}); + l4.erase(3); + REQUIRE(l4.search(3) == false); + l4.push_back(3); + REQUIRE(l4.search(3) == true); + l4.erase(3); + l4.erase(2); + REQUIRE(l4.search(2) == false); + l4.push_back(2); + REQUIRE(l4.search(2) == true); + l4.erase(2); + l4.push_front(2); + REQUIRE(l4.search(2) == true); + + doubly_linked_list l5({1, 2, 3}); + l5.erase(1); + REQUIRE(l5.search(1) == false); + l5.erase(3); + REQUIRE(l5.search(3) == false); +} + + #define LINKED_LIST_VISUALIZATION_H #ifdef LINKED_LIST_VISUALIZATION_H