Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions operations_on_datastructures/get_size_of_linked_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ int getSize(Node *root) {
return 1 + getSize(root->next);
}

/*
* @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;
}
}

int main() {
Node *myList = new Node(0, NULL); // Initializes the LinkedList
Node *temp = myList;
Expand All @@ -31,6 +45,8 @@ int main() {
std::cout << getSize(myList) << std::endl
<< getSize(secondList) << std::endl
<< getSize(thirdList) << std::endl;
deleteList(secondList);
deleteList(myList);

return 0;
}
Loading