diff --git a/ex2/ex2.c b/ex2/ex2.c index 51b4542c34b6ce80c845c2f1ed275e8df8abdc39..efe258b6975a98dc88317808c2dade49d489f633 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -16,30 +16,198 @@ #include <stdlib.h> #include <string.h> +// LinkedListNode.h +typedef struct LinkedListNode { + struct LinkedListNode *next; + int32_t value; +} LinkedListNode; + +LinkedListNode *linked_list_node_new(int32_t value); +void linked_list_node_delete(LinkedListNode **node); + +// LinkedListNode.c +LinkedListNode *linked_list_node_new(int32_t value) { + LinkedListNode *node = (LinkedListNode *)malloc(sizeof(LinkedListNode)); + + node->next = NULL; + node->value = value; + + return node; +} + +void linked_list_node_delete(LinkedListNode **node) { + free(*node); + *node = NULL; +} + +// LinkedList.h +typedef struct { + LinkedListNode *root; +} LinkedList; + +LinkedList *linked_list_new(); +void linked_list_delete(LinkedList **list); +int32_t linked_list_count(LinkedList *list); +bool linked_list_is_empty(LinkedList *list); +LinkedListNode *linked_list_first(LinkedList *list); +LinkedListNode *linked_list_last(LinkedList *list); +LinkedListNode *linked_list_add_last(LinkedList *list, int32_t value); +void linked_list_remove(LinkedList *list, LinkedListNode **node); +void linked_list_print(LinkedList *list); + +// LinkedList.c +LinkedList *linked_list_new() { + LinkedList *list = (LinkedList *)malloc(sizeof(LinkedList)); + + list->root = NULL; + + return list; +} + +void linked_list_delete(LinkedList **list) { + LinkedListNode *current_node = (*list)->root; + + while (current_node != NULL) { + LinkedListNode *current_node_copy = current_node; + current_node = current_node->next; + free(current_node_copy); + } + + free(*list); + *list = NULL; +} + +int32_t linked_list_count(LinkedList *list) { + LinkedListNode *current_node = list->root; + int32_t count = 0; + + while (current_node != NULL) { + count += 1; + current_node = current_node->next; + } + + return count; +} + +bool linked_list_is_empty(LinkedList *list) { + return linked_list_count(list) == 0; +} + +LinkedListNode *linked_list_add_last(LinkedList *list, int32_t value) { + LinkedListNode *new_node = linked_list_node_new(value); + + if (linked_list_is_empty(list)) { + list->root = new_node; + } else { + LinkedListNode *current_node = list->root; + + while (current_node->next != NULL) { + current_node = current_node->next; + } + + current_node->next = new_node; + } + + return new_node; +} + +void linked_list_remove(LinkedList *list, LinkedListNode **node) { + if (list->root == (*node)) { + list->root = (*node)->next; + } else { + LinkedListNode *current_node = list->root; + + while (current_node->next != (*node)) { + current_node = current_node->next; + } + + current_node->next = current_node->next->next; + } + + free(*node); + *node = NULL; +} + +void linked_list_print(LinkedList *list) { + LinkedListNode *current_node = list->root; + + while (current_node != NULL) { + printf("%d ", current_node->value); + current_node = current_node->next; + } + + printf("\n"); +} + +void linked_list_insert_at_index(LinkedList *list, int32_t index, int32_t value) { + if (index == 0) { + LinkedListNode *new_node = linked_list_node_new(value); + new_node->next = list->root; + list->root = new_node; + return; + } + + LinkedListNode *current_node = list->root; + + while (current_node != NULL && index != 1) { + current_node = current_node->next; + index -= 1; + } + + if (current_node == NULL) { + return; + } + + LinkedListNode *new_node = linked_list_node_new(value); + new_node->next = current_node->next; + current_node->next = new_node; +} + +void linked_list_remove_at_index(LinkedList *list, int32_t index) { + LinkedListNode *current_node = list->root; + + while (current_node != NULL && index != 0) { + current_node = current_node->next; + index -= 1; + } + + if (current_node == NULL) { + return; + } + + linked_list_remove(list, ¤t_node); +} + int main() { - // int32_t values_length = 5; - // double values[values_length]; - - // for (int32_t i = 0; i < values_length; i += 1) { - // double value; - // scanf("%lf", &value); - // values[i] = value; - // } - - // int32_t values_length = 5; - // int32_t values[values_length]; - - // for (int32_t i = 0; i < values_length; i += 1) { - // int32_t value; - // scanf("%d", &value); - // values[i] = value; - // } - - // char a[100]; - // int32_t b; - // scanf("%s %d", a, &b); - // printf("%s %d\n", a, b); - - printf("ex2\n"); + int32_t values_length = 4; + int32_t values[values_length]; + + for (int32_t i = 0; i < values_length; i += 1) { + int32_t value; + scanf("%d", &value); + values[i] = value; + } + + int32_t insert_index; + scanf("%d", &insert_index); + + int32_t remove_index; + scanf("%d", &remove_index); + + printf("\n"); + + LinkedList *list = linked_list_new(); + + for (int32_t i = 4 - 1; i >= 0; i -= 1) { + linked_list_add_last(list, values[i]); + } + + linked_list_insert_at_index(list, insert_index, 0); + linked_list_print(list); + + linked_list_remove_at_index(list, remove_index); + linked_list_print(list); + + linked_list_delete(&list); return EXIT_SUCCESS; -} +} \ No newline at end of file