Redo function signatures to match assignment doc
This commit is contained in:
parent
f69a4bf22a
commit
1031f78cfd
@ -6,8 +6,25 @@
|
|||||||
|
|
||||||
#define MAX_LEN 100
|
#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 *search(Node **head, char *data) {
|
||||||
Node *current = *head;
|
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) {
|
while (current != NULL) {
|
||||||
if (strcmp(current->data, data) == 0) {
|
if (strcmp(current->data, data) == 0) {
|
||||||
return current;
|
return current;
|
||||||
@ -17,7 +34,6 @@ Node *search(Node **head, char *data) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add contains function and use it in search
|
|
||||||
|
|
||||||
Node *searchForPrevious(Node **head, Node *current) {
|
Node *searchForPrevious(Node **head, Node *current) {
|
||||||
Node *previous = *head;
|
Node *previous = *head;
|
||||||
@ -34,25 +50,24 @@ Node *searchForPrevious(Node **head, Node *current) {
|
|||||||
void add(Node **head, char *data) {
|
void add(Node **head, char *data) {
|
||||||
if (*head == NULL) {
|
if (*head == NULL) {
|
||||||
*head = createNode(data);
|
*head = createNode(data);
|
||||||
replaceData(*head, data);
|
|
||||||
} else {
|
} else {
|
||||||
Node *current = *head;
|
Node *current = *head;
|
||||||
while (current->next_elem != NULL) {
|
while (current->next_elem != NULL) {
|
||||||
current = current->next_elem;
|
current = current->next_elem;
|
||||||
}
|
}
|
||||||
current->next_elem = createNode(data);
|
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) {
|
if (*head == NULL) {
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *result = search(head, data);
|
Node *result = search(head, data);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
return false;
|
return;
|
||||||
} else {
|
} else {
|
||||||
Node *previous = searchForPrevious(head, result);
|
Node *previous = searchForPrevious(head, result);
|
||||||
if (previous == NULL) {
|
if (previous == NULL) {
|
||||||
@ -65,22 +80,28 @@ bool delete(Node **head, char *data) {
|
|||||||
previous->next_elem = result->next_elem;
|
previous->next_elem = result->next_elem;
|
||||||
destroyNode(result);
|
destroyNode(result);
|
||||||
}
|
}
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool findAndReplace(Node **head, char *data, char *newData) {
|
void findAndReplace(Node *head, char *data, char *newData) {
|
||||||
Node *current = search(head, data);
|
Node *current = search(&head, data);
|
||||||
if (current != NULL) {
|
if (current != NULL) {
|
||||||
replaceData(current, newData);
|
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;
|
Node *current = *head;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
Node *next = current->next_elem;
|
Node *next = current->next_elem;
|
||||||
@ -89,11 +110,3 @@ bool stop(Node **head) {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printList(Node **head) {
|
|
||||||
Node *current = *head;
|
|
||||||
while (current != NULL) {
|
|
||||||
printf("%s\n", current->data);
|
|
||||||
current = current->next_elem;
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,18 +4,20 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
|
int contains(Node *head, char *data);
|
||||||
|
|
||||||
Node *search(Node **head, char *data);
|
Node *search(Node **head, char *data);
|
||||||
|
|
||||||
Node *searchForPrevious(Node **head, Node *current);
|
Node *searchForPrevious(Node **head, Node *current);
|
||||||
|
|
||||||
void add(Node **head, char *data);
|
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
|
#endif //ASSIGNMENT1_LINKED_LIST_H
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
|
|
||||||
#define MAX_LEN 100
|
#define MAX_LEN 100
|
||||||
|
|
||||||
// TODO: Make sure function signatures are correct and match the documentation
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Node *head = NULL;
|
Node *head = NULL;
|
||||||
// Double buffer to support up to 100 chars in find and replace command
|
// 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 assigns a null terminator to the end of the string, so don't need to do it manually
|
||||||
sscanf(data, "%99s %99s", input1, input2);
|
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
|
// 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') {
|
} else if (buffer[0] == 'p') {
|
||||||
printList(&head);
|
printList(head);
|
||||||
} else if (buffer[0] == 's') {
|
} else if (buffer[0] == 's') {
|
||||||
stopLoop = stop(&head);
|
stopLoop = stop(&head);
|
||||||
}
|
}
|
||||||
|
@ -4,32 +4,6 @@
|
|||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
//Assume that the string passed in is null terminated
|
//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) {
|
void replaceData(Node *node, char *data) {
|
||||||
// Free the data first, then allocate memory for the new 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");
|
printf("Error allocating memory for replacing data\n");
|
||||||
exit(EXIT_FAILURE);
|
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);
|
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);
|
||||||
|
}
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
#ifndef ASSIGNMENT1_NODE_H
|
#ifndef ASSIGNMENT1_NODE_H
|
||||||
#define ASSIGNMENT1_NODE_H
|
#define ASSIGNMENT1_NODE_H
|
||||||
|
|
||||||
typedef struct node {
|
typedef struct node {
|
||||||
struct node *next_elem;
|
struct node *next_elem;
|
||||||
char *data;
|
char *data;
|
||||||
} Node;
|
} Node;
|
||||||
|
|
||||||
|
void replaceData(Node *node, char *data);
|
||||||
|
|
||||||
Node *createNode(char *data);
|
Node *createNode(char *data);
|
||||||
|
|
||||||
void destroyNode(Node *node);
|
void destroyNode(Node *node);
|
||||||
|
|
||||||
void replaceData(Node *node, char *data);
|
|
||||||
|
|
||||||
#endif //ASSIGNMENT1_NODE_H
|
#endif //ASSIGNMENT1_NODE_H
|
||||||
|
Loading…
Reference in New Issue
Block a user