From 75e807f7f515a6d42a201b92b33bfdfdc6406223 Mon Sep 17 00:00:00 2001 From: Gajanan Choudhary Date: Thu, 6 Aug 2020 15:40:32 -0500 Subject: [PATCH] Add reverse() method to singly linked lists. Had to change const-ness of gethead() and getnext() methods to enable testing list of lists. --- include/list.h | 26 +++++++++++++++--- tests/test_list.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/include/list.h b/include/list.h index 640835f..a0bed47 100644 --- a/include/list.h +++ b/include/list.h @@ -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; } @@ -78,7 +78,7 @@ class List { data = newdata; } //! \brief Next getter function. - Node* getnext() const { + Node* getnext() { DEBUG_MSG("Node:: Getting next"); return next; } @@ -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; }; @@ -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(); + }; /******************************************************************************/ @@ -220,6 +223,23 @@ void List::pop_bottom(){ } } +//! \brief Reverse the linked list. +template +void List::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. */ diff --git a/tests/test_list.cpp b/tests/test_list.cpp index eefae07..ed6c6cf 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -33,6 +33,9 @@ TEST_CASE("Empty List") { SUBCASE("Self Move Assignment") { u = std::move(u); } + SUBCASE("Reverse") { + u.reverse(); + } CHECK(u.gethead() == nullptr); } @@ -47,6 +50,10 @@ TEST_CASE("Single-noded List") { u.push_bottom(); } } + SUBCASE("Reverse") { + u.push_top(); + u.reverse(); + } CHECK(u.gethead()->getdata() == int()); CHECK(u.gethead()->getnext() == nullptr); } @@ -76,6 +83,9 @@ TEST_CASE("Single-noded List") { 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); @@ -242,6 +252,14 @@ TEST_CASE("Multiple-noded List") { 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") { @@ -281,23 +299,23 @@ TEST_CASE("Complex Multiple-noded List>") { sentence1.push_top("world!"); sentence1.push_top("Hello"); - List sentence2; + decltype(sentence1) sentence2; sentence2.push_top("difficult!"); sentence2.push_top("is"); sentence2.push_top("OOP"); - List sentence3; + decltype(sentence1) sentence3; sentence3.push_bottom("Templates"); sentence3.push_bottom("are"); sentence3.push_bottom("beautiful!"); - List> u; + List 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 *tmp = &(u.gethead()->getdata()); CHECK(tmp->gethead()->getdata() == "Hello"); CHECK(tmp->gethead()->getnext()->getdata() == "world!"); @@ -310,5 +328,44 @@ TEST_CASE("Complex Multiple-noded List>") { 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"); + } + } }