Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/CSRT-NTUA/AlgoPlus
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Nov 13, 2024
2 parents 7c2ac38 + 050c542 commit 5361d7d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/macos_test_cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --output-on-failure
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.0.1
uses: codecov/codecov-action@v4.6.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: CSRT-NTUA/AlgoPlus
Expand Down
29 changes: 16 additions & 13 deletions src/classes/list/doubly_linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,24 @@ template <typename T> bool doubly_linked_list<T>::search(T key) {
return false;
} else {
std::shared_ptr<node> 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 <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 All @@ -196,6 +193,9 @@ template <typename T> void doubly_linked_list<T>::push_front(T key) {
if (root != nullptr) {
root->prev = p;
}
if(tail == nullptr) {
tail = p;
}
root = p;
_size++;
}
Expand All @@ -204,9 +204,6 @@ template <typename T> void doubly_linked_list<T>::erase(T key) {
if (root == nullptr) {
return;
}
if (root->val == key) {
root = root->next;
}
std::shared_ptr<node> head = root;
while (head && head->val != key) {
head = head->next;
Expand All @@ -215,9 +212,15 @@ template <typename T> void doubly_linked_list<T>::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;
}
}
Expand Down
57 changes: 57 additions & 0 deletions tests/list/doubly_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> l({1, 2, 3, 4});
REQUIRE(l.search(4) == true);
doubly_linked_list<char> 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<int> l2;
l2.push_front(1);
l2.push_back(2);
l2.push_back(3);
std::vector<int> 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<int> 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<int> 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<int> 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

Expand Down

0 comments on commit 5361d7d

Please sign in to comment.