File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
operations_on_datastructures Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,20 @@ int getSize(Node *root) {
16
16
return 1 + getSize (root->next );
17
17
}
18
18
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
+
19
33
int main () {
20
34
Node *myList = new Node (0 , NULL ); // Initializes the LinkedList
21
35
Node *temp = myList;
@@ -31,6 +45,8 @@ int main() {
31
45
std::cout << getSize (myList) << std::endl
32
46
<< getSize (secondList) << std::endl
33
47
<< getSize (thirdList) << std::endl;
48
+ deleteList (secondList);
49
+ deleteList (myList);
34
50
35
51
return 0 ;
36
52
}
You can’t perform that action at this time.
0 commit comments