From 1031f78cfd39730a15cae346563303ddee82da21 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Tue, 19 Sep 2023 15:38:03 -0300 Subject: [PATCH] Redo function signatures to match assignment doc --- Assignment1/linked_list.c | 53 ++++++++++++++++++++++++--------------- Assignment1/linked_list.h | 10 +++++--- Assignment1/main.c | 6 ++--- Assignment1/node.c | 51 ++++++++++++++++++------------------- Assignment1/node.h | 5 ++-- 5 files changed, 68 insertions(+), 57 deletions(-) diff --git a/Assignment1/linked_list.c b/Assignment1/linked_list.c index 94c9467..b3544af 100644 --- a/Assignment1/linked_list.c +++ b/Assignment1/linked_list.c @@ -6,8 +6,25 @@ #define MAX_LEN 100 +int contains(Node *head, char *data) { + Node *current = head; + while (current != NULL) { + if (strcmp(current->data, data) == 0) { + return true; + } + current = current->next_elem; + } + return false; +} + Node *search(Node **head, char *data) { Node *current = *head; + + // Only using this function here because the assignment asks for it. There is no reason to do this otherwise + if (contains(current, data) == false) { + return NULL; + } + while (current != NULL) { if (strcmp(current->data, data) == 0) { return current; @@ -17,7 +34,6 @@ Node *search(Node **head, char *data) { return NULL; } -// TODO: Add contains function and use it in search Node *searchForPrevious(Node **head, Node *current) { Node *previous = *head; @@ -34,25 +50,24 @@ Node *searchForPrevious(Node **head, Node *current) { void add(Node **head, char *data) { if (*head == NULL) { *head = createNode(data); - replaceData(*head, data); } else { Node *current = *head; while (current->next_elem != NULL) { current = current->next_elem; } current->next_elem = createNode(data); - replaceData(current->next_elem, data); } } -bool delete(Node **head, char *data) { + +void delete(Node **head, char *data) { if (*head == NULL) { - return false; + return; } Node *result = search(head, data); if (result == NULL) { - return false; + return; } else { Node *previous = searchForPrevious(head, result); if (previous == NULL) { @@ -65,22 +80,28 @@ bool delete(Node **head, char *data) { previous->next_elem = result->next_elem; destroyNode(result); } - return true; + return; } } -bool findAndReplace(Node **head, char *data, char *newData) { - Node *current = search(head, data); +void findAndReplace(Node *head, char *data, char *newData) { + Node *current = search(&head, data); if (current != NULL) { replaceData(current, newData); - return true; } - return false; } -bool stop(Node **head) { +void printList(Node *head) { + Node *current = head; + while (current != NULL) { + printf("%s\n", current->data); + current = current->next_elem; + } +} + +int stop(Node **head) { Node *current = *head; while (current != NULL) { Node *next = current->next_elem; @@ -89,11 +110,3 @@ bool stop(Node **head) { } return true; } - -void printList(Node **head) { - Node *current = *head; - while (current != NULL) { - printf("%s\n", current->data); - current = current->next_elem; - } -} \ No newline at end of file diff --git a/Assignment1/linked_list.h b/Assignment1/linked_list.h index 1fbfe04..eba5f55 100644 --- a/Assignment1/linked_list.h +++ b/Assignment1/linked_list.h @@ -4,18 +4,20 @@ #include #include "node.h" +int contains(Node *head, char *data); + Node *search(Node **head, char *data); Node *searchForPrevious(Node **head, Node *current); void add(Node **head, char *data); -bool delete(Node **head, char *data); +void delete(Node **head, char *data); -bool findAndReplace(Node **head, char *data, char *newData); +void findAndReplace(Node *head, char *data, char *newData); -void printList(Node **head); +void printList(Node *head); -bool stop(Node **head); +int stop(Node **head); #endif //ASSIGNMENT1_LINKED_LIST_H diff --git a/Assignment1/main.c b/Assignment1/main.c index a6262d4..896ae2d 100644 --- a/Assignment1/main.c +++ b/Assignment1/main.c @@ -5,8 +5,6 @@ #define MAX_LEN 100 -// TODO: Make sure function signatures are correct and match the documentation - int main() { Node *head = NULL; // Double buffer to support up to 100 chars in find and replace command @@ -33,9 +31,9 @@ int main() { // sscanf assigns a null terminator to the end of the string, so don't need to do it manually sscanf(data, "%99s %99s", input1, input2); // Could check if the replacement was successful by using return value of function, but I don't have to - findAndReplace(&head, input1, input2); + findAndReplace(head, input1, input2); } else if (buffer[0] == 'p') { - printList(&head); + printList(head); } else if (buffer[0] == 's') { stopLoop = stop(&head); } diff --git a/Assignment1/node.c b/Assignment1/node.c index 258c470..6da957e 100644 --- a/Assignment1/node.c +++ b/Assignment1/node.c @@ -4,32 +4,6 @@ #include "node.h" //Assume that the string passed in is null terminated -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); - } - // Allocate memory for the data, and check if it was successful - node->data = calloc(strlen(data) + 1, sizeof(char)); - if (node->data == NULL) { - printf("Error allocating memory for node 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->next_elem = NULL; - return node; -} - -void destroyNode(Node *node) { - // Free the data first, then free the node - free(node->data); - free(node); -} void replaceData(Node *node, char *data) { // Free the data first, then allocate memory for the new data @@ -39,6 +13,29 @@ void replaceData(Node *node, char *data) { printf("Error allocating memory for replacing data\n"); exit(EXIT_FAILURE); } - // Same rational as for createNode + // 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); +} diff --git a/Assignment1/node.h b/Assignment1/node.h index e22fad5..246bb91 100644 --- a/Assignment1/node.h +++ b/Assignment1/node.h @@ -1,14 +1,15 @@ #ifndef ASSIGNMENT1_NODE_H #define ASSIGNMENT1_NODE_H + typedef struct node { struct node *next_elem; char *data; } Node; +void replaceData(Node *node, char *data); + Node *createNode(char *data); void destroyNode(Node *node); -void replaceData(Node *node, char *data); - #endif //ASSIGNMENT1_NODE_H