#include #include #include #include "node.h" //Assume that the string passed in is null terminated void replaceData(Node *node, char *data) { // Free the data first, then allocate memory for the new data free(node->data); node->data = calloc(strlen(data) + 1, sizeof(char)); if (node->data == NULL) { printf("Error allocating memory for replacing data\n"); exit(EXIT_FAILURE); } // Copy data from the string passed in to the node's data // Makes sure if the string passed in is changed, the node's data is not changed // Also makes sure that if the data is on the stack, it is not freed at some other point strcpy(node->data, data); } Node *createNode(char *data) { // Allocate memory for the node, and check if it was successful Node *node = calloc(1, sizeof(Node)); if (node == NULL) { printf("Error allocating memory for node\n"); exit(EXIT_FAILURE); } // replaceData allocates memory for the data, checks if it was successful, and copies the data // So it would be redundant to reimplement that here replaceData(node, data); node->next_elem = NULL; return node; } void destroyNode(Node *node) { // Free the data first, then free the node free(node->data); free(node); }