From ee13b00dce7aed85854e8c4d2d7e631331c48988 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 2 Nov 2021 13:37:17 +0530 Subject: [PATCH] Fixed copy and move operators/constructors and added documentation --- .../circular_linked_list.cpp | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/operations_on_datastructures/circular_linked_list.cpp b/operations_on_datastructures/circular_linked_list.cpp index 2ddb626db..3c60fb29a 100644 --- a/operations_on_datastructures/circular_linked_list.cpp +++ b/operations_on_datastructures/circular_linked_list.cpp @@ -71,25 +71,57 @@ class CircularLinkedList { * @brief Copy constructor for CircularLinkedList. */ CircularLinkedList(const CircularLinkedList& copy) { - root = copy.root; - end = copy.end; + erase(); + root = nullptr; + Node* node = copy.root; + while (node != nullptr) { + insert(node->data); + node = node->next; + } } + /** + * @brief Move constructor for CircularLinkedList + * @param source rvalue reference to a Circular Linked List + */ CircularLinkedList(CircularLinkedList&& source) { root = source.root; end = source.end; + source.root = nullptr; + source.end = nullptr; } + /** + * @brief Copy assignment operator + * @param other Reference to a Circular Linked List + * @returns Reference to CircularLinkedList + */ CircularLinkedList& operator=(const CircularLinkedList& other) { - root = other.root; - end = other.end; + erase(); + root = nullptr; + Node* node = other.root; + while (node != nullptr) { + insert(node->data); + node = node->next; + } } + /** + * @brief Move assignment operator + * @param other rvalue reference to a Circular Linked List + * @returns Reference to CircularLinkedList + */ CircularLinkedList& operator=(CircularLinkedList&& other) { root = other.root; end = other.end; + other.root = nullptr; + other.end = nullptr; } /** - * @brief Cleans up memory + * @brief Cleans up memory when destroyed */ - ~CircularLinkedList() { + ~CircularLinkedList() { erase(); } + /** + * Iteratively frees each node in the Circular Linked List from the heap + */ + void erase() { if (root == nullptr) { return; } @@ -99,6 +131,8 @@ class CircularLinkedList { node = node->next; delete (temp); } while (node != root); + root = nullptr; + end = nullptr; } /** * @brief Inserts all the values from a vector into the Circular Linked List