Skip to content

Commit

Permalink
Fixed another bug with erase
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Nov 12, 2024
1 parent 7ebbd5e commit 684b4ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/classes/list/doubly_linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ template <typename T> bool doubly_linked_list<T>::search(T key) {

template <typename T> void doubly_linked_list<T>::push_back(T key) {
std::shared_ptr<node> p = std::make_shared<node>(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++;
}
Expand Down Expand Up @@ -215,9 +215,15 @@ template <typename T> void doubly_linked_list<T>::erase(T key) {
return;
}
if (head->next != nullptr) {
if(head == root) {
root = root -> next;

Check warning on line 219 in src/classes/list/doubly_linked_list.h

View check run for this annotation

Codecov / codecov/patch

src/classes/list/doubly_linked_list.h#L219

Added line #L219 was not covered by tests
}
head->next->prev = head->prev;
}
if (head->prev != nullptr) {
if(head == tail) {
tail = tail->prev;
}
head->prev->next = head->next;
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/list/doubly_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ TEST_CASE("Testing searching for issue #88") {
for(auto it = l2.begin(); it != l2.end(); it++) {
REQUIRE(*(it) == check[idx++]);
}
std::cout << "im here as well" << '\n';

// Bug in erase method
doubly_linked_list<int> l3({1, 2, 3});
Expand All @@ -166,6 +165,15 @@ TEST_CASE("Testing searching for issue #88") {
for(auto it = l3.begin(); it != l3.end(); it++) {
REQUIRE(*(it) == check[idx++]);
}

doubly_linked_list<int> l4({1, 2, 3});
l4.erase(3);
REQUIRE(l4.search(3) == false);
l4.push_back(3);
for(auto it = l4.begin(); it != l4.end(); it++) {
std::cout << *(it) << ' ';
}
REQUIRE(l4.search(3) == true);
}


Expand Down

0 comments on commit 684b4ec

Please sign in to comment.