Redo function signatures to match assignment doc

This commit is contained in:
Isaac Shoebottom 2023-09-19 15:38:03 -03:00
parent f69a4bf22a
commit 1031f78cfd
5 changed files with 68 additions and 57 deletions

View File

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

View File

@ -4,18 +4,20 @@
#include <stdbool.h>
#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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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