Skip to content

Commit

Permalink
Fixed bug with linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jul 29, 2024
1 parent 3fdf875 commit d9b08e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/classes/list/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ template <typename T> void linked_list<T>::push_back(T key) {
template <typename T> void linked_list<T>::push_front(T key) {
std::shared_ptr<node> p = std::make_shared<node>(key);
p->next = root;
if(tail == nullptr) { tail = root; }
root = p;
_size++;
}
Expand Down
17 changes: 16 additions & 1 deletion tests/list/list.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define CATCH_CONFIG_MAIN
#include "../../src/classes/list/linked_list.h"
#include "../../third_party/catch.hpp"
#include <string>
Expand Down Expand Up @@ -92,4 +93,18 @@ TEST_CASE("testing operator = in single list") {
l2 = l;
std::vector<int> v2 = l2.elements();
REQUIRE(v2 == v);
}
}

TEST_CASE("Bug with tail when pushing front and then pushing back") {
linked_list<char> l;
l.push_front('d');
l.push_front('c');
l.push_front('b');
l.push_front('a');
l.push_back('e');
l.push_back('f');

std::vector<char> v = l.elements();
std::vector<char> check = {'a', 'b', 'c', 'd', 'e', 'f'};
REQUIRE(v == check);
}

0 comments on commit d9b08e6

Please sign in to comment.