From 2bc5dfc0e1ae78be3f57af1dbe60ee20680e3ff4 Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Fri, 13 Feb 2026 10:34:23 -0600 Subject: [PATCH] Added pop function, same as delete but writes out the value, lowkey i'll do the tests later because delete pretty much already does the same thing --- src/linkedlist.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/linkedlist.c b/src/linkedlist.c index 7f8e43e..a309a86 100644 --- a/src/linkedlist.c +++ b/src/linkedlist.c @@ -206,3 +206,40 @@ LinkedListErr linkedlist_len(const LinkedList *ll, size_t *out) { *out = ll->len; return LLIST_OK; } + +LinkedListErr linkedlist_pop(LinkedList *ll, size_t index, void *out) { + if (ll == NULL) { + return LLIST_NULL; + } + + if (out == NULL) { + return LLIST_NULL_ARG; + } + + if (index >= ll->len) { + return LLIST_OUT_OF_BOUNDS; + } + + Node *before = NULL; + Node *current = ll->head; + for (size_t i = 0; i < index; i++) { + before = current; + current = current->next; + } + + if (index == 0) { + ll->head = current->next; + } else { + // If its last current-next is just NULL so is okay + before->next = current->next; + } + + memcpy(out, current->data, ll->elem_size); + // Fuck them dangling pointers + free(current->data); + free(current); + ll->len = ll->len - 1; + + return LLIST_OK; + +}