From 1644e4064123b5fc9f66d60d8c0717e36aef23f8 Mon Sep 17 00:00:00 2001 From: SleepiCaffeine Date: Tue, 20 Jun 2023 19:19:00 +0300 Subject: [PATCH] Fixed bugs in double_node and dl_list classes --- Data Structures/dl_list.hpp | 38 +++++++++++++++++++++++++++------ Data Structures/double_node.hpp | 10 ++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Data Structures/dl_list.hpp b/Data Structures/dl_list.hpp index d0f59d2..32481a9 100644 --- a/Data Structures/dl_list.hpp +++ b/Data Structures/dl_list.hpp @@ -13,6 +13,7 @@ #include "double_node.hpp" #include #include +#include /*! * @class dl_list @@ -260,14 +261,18 @@ class dl_list { * @return Length of LL * @see size() */ - unsigned int length() const {return len;} + unsigned int length() { + update_len(); + return len;} /** * Returns the length of the sl list * @return Length of LL * @see length() */ - unsigned int size() const {return len;} + unsigned int size() { + update_len(); + return len;} /** * Updates the length of the sl_list (Mainly kept to avoid bugs, will later implement other features to avoid using this) @@ -332,6 +337,15 @@ double_node* dl_list::push_front(T dt) { ++len; return head; } + + if (len == 1) { + tail = head; + head = new double_node(dt); + head->set_next(tail); + tail->set_prev(head); + ++len; + return head; + } // Create a new head object with the provided data and pointing to the head itself head = new double_node(head, dt); ++len; @@ -342,16 +356,26 @@ template double_node* dl_list::push_front(double_node* const nd) { // If the list has a length of 0, create a node for the head, and set the tail to nullptr if (!len) { - head = new double_node(*nd); + head = new double_node(*nd); tail = nullptr; ++len; return head; } + if (len == 1) { + tail = head; + head = new double_node(*nd); + head->set_next(tail); + tail->set_prev(head); + ++len; + return head; + } + // Swap head and provided node - auto oldHead = new double_node(head->get_next(), nd, head->get_data()); + auto oldHead = new double_node(*head); + oldHead->set_prev(nd); nd->set_next(oldHead); - head = nd; + head = new double_node(*nd); ++len; return head; @@ -411,7 +435,9 @@ double_node* dl_list::push_back(double_node* const nd) { } double_node* currNode = head; - double_node* oldTail = new double_node(nd, tail->get_prev(), tail->get_data()); + double_node* oldTail = new double_node(*tail); + oldTail->set_next(nd); + nd->set_prev(oldTail); tail = nd; diff --git a/Data Structures/double_node.hpp b/Data Structures/double_node.hpp index c23bf31..6ebd2c9 100644 --- a/Data Structures/double_node.hpp +++ b/Data Structures/double_node.hpp @@ -192,14 +192,8 @@ class double_node { }; template -double_node::double_node(const double_node& nd) { - next = new double_node(); - prev = new double_node(); - - next = nd.get_next(); - prev = nd.get_prev(); - data = nd.get_data(); -} +double_node::double_node(const double_node& nd) + : next{nd.get_next()}, prev{nd.get_prev()}, data{nd.get_data()} { } template void double_node::set_prev(double_node* const nd) {