mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-14 07:55:11 +08:00
fix: remove memory leak in sublist_search.cpp (#2541)
Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
This commit is contained in:
@@ -90,6 +90,20 @@ Node *makeLinkedList(const std::vector<uint64_t> &data) {
|
||||
return head;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief This function dealocates memory related to the given list
|
||||
* It recursively deletes all of the nodes of the input list.
|
||||
* @param room the root/head of the input list
|
||||
* @warning Plese note that the memory for each node has to be alocated using
|
||||
* new.
|
||||
*/
|
||||
void deleteList(Node *const root) {
|
||||
if (root != NULL) {
|
||||
deleteList(root->next);
|
||||
delete root;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main searching function
|
||||
* @param sublist A linked list which is supposed to be searched in mainList.
|
||||
@@ -217,8 +231,8 @@ class TestCases {
|
||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
||||
"~");
|
||||
|
||||
delete (sublistLL);
|
||||
delete (mainlistLL);
|
||||
deleteList(mainlistLL);
|
||||
deleteList(sublistLL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -270,6 +284,9 @@ class TestCases {
|
||||
log("[PASS] : TEST CASE 2 PASS!");
|
||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
||||
"~");
|
||||
|
||||
deleteList(mainlistLL);
|
||||
deleteList(sublistLL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -318,6 +335,9 @@ class TestCases {
|
||||
log("[PASS] : TEST CASE 3 PASS!");
|
||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
||||
"~");
|
||||
|
||||
deleteList(mainlistLL);
|
||||
deleteList(sublistLL);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -366,5 +386,8 @@ int main(int argc, char *argv[]) {
|
||||
} else {
|
||||
std::cout << "[FALSE] - sublist NOT found in main list\n";
|
||||
}
|
||||
|
||||
deleteList(mainlistLL);
|
||||
deleteList(sublistLL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user