Fixed copy and move operators/constructors and added documentation

This commit is contained in:
Alvin Philips 2021-11-02 13:37:17 +05:30
parent b36e4736f5
commit ee13b00dce

View File

@ -71,25 +71,57 @@ class CircularLinkedList {
* @brief Copy constructor for CircularLinkedList. * @brief Copy constructor for CircularLinkedList.
*/ */
CircularLinkedList(const CircularLinkedList& copy) { CircularLinkedList(const CircularLinkedList& copy) {
root = copy.root; erase();
end = copy.end; 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) { CircularLinkedList(CircularLinkedList&& source) {
root = source.root; root = source.root;
end = source.end; 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) { CircularLinkedList& operator=(const CircularLinkedList& other) {
root = other.root; erase();
end = other.end; 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) { CircularLinkedList& operator=(CircularLinkedList&& other) {
root = other.root; root = other.root;
end = other.end; 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) { if (root == nullptr) {
return; return;
} }
@ -99,6 +131,8 @@ class CircularLinkedList {
node = node->next; node = node->next;
delete (temp); delete (temp);
} while (node != root); } while (node != root);
root = nullptr;
end = nullptr;
} }
/** /**
* @brief Inserts all the values from a vector into the Circular Linked List * @brief Inserts all the values from a vector into the Circular Linked List