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

This commit is contained in:
2026-02-13 10:34:23 -06:00
parent 7c783c8a23
commit 2bc5dfc0e1

View File

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