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