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:
@@ -206,3 +206,40 @@ LinkedListErr linkedlist_len(const LinkedList *ll, size_t *out) {
|
|||||||
*out = ll->len;
|
*out = ll->len;
|
||||||
return LLIST_OK;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user