IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 5: [Data Structures] – Structures, Singly-Linked Lists, Doubly-Linked Lists, Hash Tables, Tries, Queues, and Stacks › Bool unload(void) function. is the parameter for it set void because it is taking input of root node from global variable root?
- This topic is empty.
-
AuthorPosts
-
November 12, 2023 at 5:31 am #1760// Saves popular dog names in a trie#include <cs50.h>#include <ctype.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#define SIZE_OF_ALPHABET 26#define MAXCHAR 20typedef struct node{bool is_word;struct node *children[SIZE_OF_ALPHABET];} node;// Function prototypesbool check(char *word);bool unload(void);void unloader(node *current);// Root of trienode *root;// Buffer to read dog names intochar name[MAXCHAR];int main(int argc, char *argv[]){// Check for command line argsif (argc != 2){printf(“Usage: ./trie infile\n”);return 1;}// File with namesFILE *infile = fopen(argv[1], “r”);if (!infile){printf(“Error opening file!\n”);return 1;}// Allocate root of trieroot = malloc(sizeof(node));if (root == NULL){return 1;}root->is_word = false;for (int i = 0; i < SIZE_OF_ALPHABET; i++){root->children[i] = NULL;}// Add words to the triewhile (fscanf(infile, “%s”, name) == 1){node *cursor = root;for (int i = 0, n = strlen(name); i < n; i++){int index = tolower(name[i]) – ‘a’;if (cursor->children[index] == NULL){// Make nodenode *new = malloc(sizeof(node));new->is_word = false;for (int j = 0; j < SIZE_OF_ALPHABET; j++){new->children[j] = NULL;}cursor->children[index] = new;}// Go to node which we may have just been madecursor = cursor->children[index];}// if we are at the end of the word, mark it as being a wordcursor->is_word = true;}if (check(get_string(“Check word: “))){printf(“Found!\n”);}else{printf(“Not Found.\n”);}if (!unload()){printf(“Problem freeing memory!\n”);return 1;}fclose(infile);}// TODO: Complete the check function, return true if found, false if not foundbool check(char *word){strcmp()return true;}return false;}// Unload trie from memorybool unload(void){// The recursive function handles all of the freeingunloader(root);return true;}void unloader(node *current){// Iterate over all the children to see if they point to anything and go// there if they do pointfor (int i = 0; i < SIZE_OF_ALPHABET; i++){if (current->children[i] != NULL){unloader(current->children[i]);}}// After we check all the children point to null we can get rid of the node// and return to the previous iteration of this function.free(current);}My query is regarding bool unload(void); function. Is the parameter for it set void because it is taking input of root node from global variable root.
Replied by ChatGPT:
Yes, the `unload` function is designed to take no parameters because it is intended to operate on the global variable `root`. In this case, `root` is a pointer to the root of the trie data structure. The `unload` function uses a recursive approach to free the memory associated with each node in the trie.
Since the `root` variable is a global variable, it can be accessed directly within the `unload` function without the need to pass it as a parameter. This is a common practice when working with tree structures or other data structures where you need to traverse and modify nodes recursively.
In C, when you pass a pointer to a function, you’re passing a copy of the pointer’s value, not the actual pointer itself. Since you want to modify the original pointer (set it to `NULL` after freeing all nodes), it’s more convenient to work with the global variable directly. That’s why the `unload` function takes no parameters.
-
AuthorPosts
- You must be logged in to reply to this topic.