Skip to content

Commit 3ad3363

Browse files
Merge branch 'master' into memory_issues_in_counting_sort_patch
2 parents 9c29933 + e139ee9 commit 3ad3363

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

math/iterative_factorial.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace math {
4646
*/
4747
uint64_t iterativeFactorial(uint8_t n) {
4848
if (n > 20) {
49-
throw new std::invalid_argument("Maximum n value is 20");
49+
throw std::invalid_argument("Maximum n value is 20");
5050
}
5151

5252
// 1 because it is the identity number of multiplication.
@@ -101,12 +101,14 @@ static void test() {
101101
std::cout << "Exception test \n"
102102
"Input: 21 \n"
103103
"Expected output: Exception thrown \n";
104+
105+
bool wasExceptionThrown = false;
104106
try {
105107
math::iterativeFactorial(21);
106-
} catch (std::invalid_argument* e) {
107-
std::cout << "Exception thrown successfully \nContent: " << e->what()
108-
<< "\n";
108+
} catch (const std::invalid_argument&) {
109+
wasExceptionThrown = true;
109110
}
111+
assert(wasExceptionThrown);
110112

111113
std::cout << "All tests have passed successfully.\n";
112114
}

operations_on_datastructures/get_size_of_linked_list.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ int getSize(Node *root) {
1616
return 1 + getSize(root->next);
1717
}
1818

19+
/*
20+
* @brief This function dealocates memory related to the given list
21+
* It recursively deletes all of the nodes of the input list.
22+
* @param room the root/head of the input list
23+
* @warning Plese note that the memory for each node has to be alocated using new.
24+
*/
25+
void deleteList(Node *const root) {
26+
if (root != NULL)
27+
{
28+
deleteList(root->next);
29+
delete root;
30+
}
31+
}
32+
1933
int main() {
2034
Node *myList = new Node(0, NULL); // Initializes the LinkedList
2135
Node *temp = myList;
@@ -31,6 +45,8 @@ int main() {
3145
std::cout << getSize(myList) << std::endl
3246
<< getSize(secondList) << std::endl
3347
<< getSize(thirdList) << std::endl;
48+
deleteList(secondList);
49+
deleteList(myList);
3450

3551
return 0;
3652
}

0 commit comments

Comments
 (0)