Skip to content

Commit

Permalink
Add reverse() method to singly linked lists.
Browse files Browse the repository at this point in the history
Had to change const-ness of gethead() and getnext() methods to enable
testing list of lists.
  • Loading branch information
gajanan-choudhary committed Aug 6, 2020
1 parent f6a4ae6 commit 75e807f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
26 changes: 23 additions & 3 deletions include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class List {
Node & operator=(Node &&) = delete;

//! \brief Data Getter function.
const T & getdata() const {
T & getdata() {
DEBUG_MSG("Node:: Getting data");
return data;
}
Expand All @@ -78,7 +78,7 @@ class List {
data = newdata;
}
//! \brief Next getter function.
Node* getnext() const {
Node* getnext() {
DEBUG_MSG("Node:: Getting next");
return next;
}
Expand All @@ -96,7 +96,7 @@ class List {
Node *tail; //! Never allocated. Only pointed to.

public:
Node *gethead() const {
Node *gethead() {
DEBUG_MSG("List:: Getting head");
return head;
};
Expand Down Expand Up @@ -125,6 +125,9 @@ class List {
//! \brief Remove node from the end/tail of the list object.
void pop_bottom();

//! \brief Reverse the linked list.
void reverse();

};

/******************************************************************************/
Expand Down Expand Up @@ -220,6 +223,23 @@ void List<T>::pop_bottom(){
}
}

//! \brief Reverse the linked list.
template <typename T>
void List<T>::reverse(){
DEBUG_MSG("List:: Reversing the linked list");
if (head!=nullptr){
decltype(head) prev = nullptr;
while (head->next!=nullptr){
auto tmp = head->next;
head->next = prev;
prev = head;
head = tmp;
}
head->next = prev;
}
}


/**********************************************************************/
/** Overloaded Operator. */

Expand Down
65 changes: 61 additions & 4 deletions tests/test_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ TEST_CASE("Empty List<int>") {
SUBCASE("Self Move Assignment") {
u = std::move(u);
}
SUBCASE("Reverse") {
u.reverse();
}
CHECK(u.gethead() == nullptr);
}

Expand All @@ -47,6 +50,10 @@ TEST_CASE("Single-noded List<int>") {
u.push_bottom();
}
}
SUBCASE("Reverse") {
u.push_top();
u.reverse();
}
CHECK(u.gethead()->getdata() == int());
CHECK(u.gethead()->getnext() == nullptr);
}
Expand Down Expand Up @@ -76,6 +83,9 @@ TEST_CASE("Single-noded List<int>") {
SUBCASE("Self Move Assignment") {
u = std::move(u);
}
SUBCASE("Reverse") {
u.reverse();
}
CHECK(u.gethead() != nullptr);
CHECK(u.gethead()->getdata() == 5);
CHECK(u.gethead()->getnext() == nullptr);
Expand Down Expand Up @@ -242,6 +252,14 @@ TEST_CASE("Multiple-noded List<int>") {
CHECK(u.gethead()!=v.gethead());
CHECK((u.gethead()+1)!=(v.gethead()+1)); // Tail
}
SUBCASE("Reverse the list"){
u.reverse();
DEBUG_MSG("Printing reversed Linked list: "<< u);
CHECK(u.gethead()->getdata() == 5);
CHECK(u.gethead()->getnext()->getdata() == 1);
CHECK(u.gethead()->getnext()->getnext()->getdata() == 25);
CHECK(u.gethead()->getnext()->getnext()->getnext() == nullptr);
}
}

TEST_CASE("Multiple-noded List<string>") {
Expand Down Expand Up @@ -281,23 +299,23 @@ TEST_CASE("Complex Multiple-noded List<List<string>>") {
sentence1.push_top("world!");
sentence1.push_top("Hello");

List<string> sentence2;
decltype(sentence1) sentence2;
sentence2.push_top("difficult!");
sentence2.push_top("is");
sentence2.push_top("OOP");

List<string> sentence3;
decltype(sentence1) sentence3;
sentence3.push_bottom("Templates");
sentence3.push_bottom("are");
sentence3.push_bottom("beautiful!");

List<List<string>> u;
List<decltype(sentence1)> u;
u.push_top(sentence3);
u.push_top(sentence2);
u.push_top(sentence1);

DEBUG_MSG("Printing Linked list: "<< u);
auto *tmp = &(u.gethead()->getdata());
List<string> *tmp = &(u.gethead()->getdata());
CHECK(tmp->gethead()->getdata() == "Hello");
CHECK(tmp->gethead()->getnext()->getdata() == "world!");

Expand All @@ -310,5 +328,44 @@ TEST_CASE("Complex Multiple-noded List<List<string>>") {
CHECK(tmp->gethead()->getdata() == "Templates");
CHECK(tmp->gethead()->getnext()->getdata() == "are");
CHECK(tmp->gethead()->getnext()->getnext()->getdata() == "beautiful!");

SUBCASE("Reverse list-of-lists."){
u.reverse();
DEBUG_MSG("Printing reversed Linked list: "<< u);
auto *tmp = &(u.gethead()->getdata());
CHECK(tmp->gethead()->getdata() == "Templates");
CHECK(tmp->gethead()->getnext()->getdata() == "are");
CHECK(tmp->gethead()->getnext()->getnext()->getdata() == "beautiful!");

tmp = &(u.gethead()->getnext()->getdata());
CHECK(tmp->gethead()->getdata() == "OOP");
CHECK(tmp->gethead()->getnext()->getdata() == "is");
CHECK(tmp->gethead()->getnext()->getnext()->getdata() == "difficult!");

tmp = &(u.gethead()->getnext()->getnext()->getdata());
CHECK(tmp->gethead()->getdata() == "Hello");
CHECK(tmp->gethead()->getnext()->getdata() == "world!");
SUBCASE("Reverse lists inside list-of-lists."){
auto *tmp1 = &(u.gethead()->getdata());
tmp1->reverse();
DEBUG_MSG("Printing reversed Linked list: "<< u);
CHECK(tmp1->gethead()->getdata() == "beautiful!");
CHECK(tmp1->gethead()->getnext()->getdata() == "are");
CHECK(tmp1->gethead()->getnext()->getnext()->getdata() == "Templates");

tmp1 = &(u.gethead()->getnext()->getdata());
tmp1->reverse();
DEBUG_MSG("Printing reversed Linked list: "<< u);
CHECK(tmp1->gethead()->getdata() == "difficult!");
CHECK(tmp1->gethead()->getnext()->getdata() == "is");
CHECK(tmp1->gethead()->getnext()->getnext()->getdata() == "OOP");

tmp1 = &(u.gethead()->getnext()->getnext()->getdata());
tmp1->reverse();
DEBUG_MSG("Printing reversed Linked list: "<< u);
CHECK(tmp1->gethead()->getdata() == "world!");
CHECK(tmp1->gethead()->getnext()->getdata() == "Hello");
}
}
}

0 comments on commit 75e807f

Please sign in to comment.