Add find and delete operations for doubly-linked list

This commit is contained in:
William Brawner 2024-11-18 17:55:01 -07:00
parent 6fa8c4a110
commit 0c8b8b251d
Signed by: wbrawner
GPG key ID: 8FF12381C6C90D35
3 changed files with 74 additions and 1 deletions

View file

@ -6,6 +6,7 @@ struct dll_node {
typedef struct dll_node dll_node;
// TODO: I probably don't need a separate "list" type
typedef struct {
struct dll_node *head;
} dll_list;
@ -33,7 +34,17 @@ void dll_list_append(dll_list *list, dll_node *node) {
dll_list_insert(list, node, -1);
}
// TODO: find and delete operations on list
/**
* Searches the given list for a node with a given value
* @returns a pointer to the node with the value or NULL if no nodes match the value
*/
dll_node *dll_list_find(dll_list *list, char *value);
/**
* Removes the first node with the matching value in the given list
* @returns a pointer to the node with the value or NULL if no nodes match the value
*/
dll_node *dll_list_delete(dll_list *list, char *value);
dll_node *dll_node_new(char *value);

View file

@ -50,6 +50,39 @@ void dll_list_insert(dll_list *list, dll_node *node, int index) {
current_node->next = node;
}
dll_node *dll_list_find(dll_list *list, char *value) {
if (list == NULL) {
return NULL;
}
dll_node *current_node = list->head;
while (current_node != NULL) {
if (strcmp(current_node->value, value) == 0) {
return current_node;
}
current_node = current_node->next;
}
return NULL;
}
dll_node *dll_list_delete(dll_list *list, char *value) {
dll_node *target_node = dll_list_find(list, value);
if (target_node == NULL) {
return NULL;
}
if (target_node->previous != NULL) {
target_node->previous->next = target_node->next;
} else {
list->head = target_node->next;
}
if (target_node->next != NULL) {
target_node->next->previous = target_node->previous;
}
return target_node;
}
dll_node *dll_node_new(char *value) {
dll_node *node = malloc(sizeof(dll_node));
node->previous = NULL;

View file

@ -8,6 +8,17 @@ dll_list *list = NULL;
dll_node *head = NULL;
char *list_string = NULL;
#define ASSERT_PTR_EQ(expected, actual) \
{ \
if (expected != actual) { \
fprintf(stderr, \
"pointers don't match. expected: \"%p\", actual: \"%p\"\n", \
(void*)expected, (void*)actual); \
cleanup(); \
return 1; \
} \
}
#define ASSERT_STR_EQ(expected, actual) \
{ \
if (strcmp(expected, actual) != 0) { \
@ -71,6 +82,7 @@ int main(int argc, char **argv) {
dll_list_append(list, head);
head = NULL;
// TODO: Extract test utilities to shared file
// TODO: Maybe break these out into separate files?
list_string = list_to_string(list);
ASSERT_STR_EQ("0", list_string);
@ -88,6 +100,23 @@ int main(int argc, char **argv) {
list_string = list_to_string(list);
ASSERT_STR_EQ("0, 1, 2", list_string);
ASSERT_PTR_EQ(second, dll_list_find(list, "1"));
ASSERT_PTR_EQ(third, dll_list_delete(list, "2"));
dll_node_free(third);
free(list_string);
list_string = list_to_string(list);
ASSERT_STR_EQ("0, 1", list_string);
head = list->head;
ASSERT_PTR_EQ(head, dll_list_delete(list, "0"));
dll_node_free(head);
free(list_string);
list_string = list_to_string(list);
ASSERT_STR_EQ("1", list_string);
cleanup();
return 0;
}