diff --git a/array_backed_list.c b/array_backed_list.c index 49594ea..11691e8 100644 --- a/array_backed_list.c +++ b/array_backed_list.c @@ -348,4 +348,75 @@ void list_clear(IntList list) { if (list_is_valid(list)) { list->size = 0; } +} + +/** + * Appends the `list_to_append` at the end of the given `list`. + * The appended list is empty afterwards, because all nodes of that list + * have been transferred to `list`. + * + * @param list The list that receives the other list. + * @param list_to_append The list that is appended to `list`. + */ +void list_append(IntList list, IntList list_to_append) { + if (list_is_valid(list) && list_is_valid(list_to_append)) { + for (int i = 0; i < list_to_append->size; i++) { + list_insert(list, list_to_append->buffer[i]); + } + list_to_append->size = 0; + } +} + +/** + * Splits the given `list` into a `left` and `right` part at the given `split_idx`. + * The `left` list contains the items of `list` from head to `split_idx - 1` rendering + * the size of the left part equal to `split_idx`. The `right` list contains + * the items of `list` from originally `split_idx` to tail. + * + * If `split_idx` is larger than the size of `list`, all items of `list` + * are moved to `left`, leaving the `right` list empty. + * + * If a target list (`left`, `right`) is not empty, the nodes that are received from + * `list` are appended. + * + * If any of the given lists is invalid, the invocation is ignored. + * + * @param list The list to split. + * @param left The left part of the original list. + * @param right The right part of the original list. + * @param split_idx The index of the node of `list` that becomes the head of `right` list. + */ +void list_split(IntList list, IntList left, IntList right, unsigned int split_idx) { + if (list_is_valid(list) && list_is_valid(left) && list_is_valid(right)) { + if (split_idx >= list->size) { + split_idx = list->size; + } + + for (int i = 0; i < split_idx; i++) { + list_insert(left, list->buffer[i]); + } + + for (int i = split_idx; i < list->size; i++) { + list_insert(right, list->buffer[i]); + } + + list_clear(list); + } +} + +/** + * Transfers the first node (the 'head') of the given `list` as last node ('tail') + * of the target list. + * + * If any of the given lists is invalid or the source list is empty, the invocation is ignored. + * + * @param list The list from which the `head` is transferred to `target_list`. + * @param target_list The list that receives the head of `list`. + */ +void list_transfer_head(IntList list, IntList target_list) { + if (list_is_valid(list) && list_is_valid(target_list) && !list_is_empty(list)) { + int value = list->buffer[0]; + list_remove_at(list, 0); + list_insert(target_list, value); + } } \ No newline at end of file