diff --git a/circ_list.c b/circ_list.c index adf0841e8e0193dae81b1dcbf60678ca532562a6..a36e82ff3a78ba8b474dcae6d8f95c109daf0baa 100644 --- a/circ_list.c +++ b/circ_list.c @@ -1,126 +1,136 @@ -#include "circ_list.h" - -#include <stdlib.h> - -element *list_create() { - return NULL; -} - -bool list_empty(element *head) { - return head == NULL; -} - -unsigned int list_count(element *head) { - if (list_empty(head)) { - return 0; - } - - unsigned int count = 0; - element *iterator = head; - - do { - count += 1; - iterator = iterator->next; - } while (iterator != head); - - return count; -} - -element *list_move(element *head) { - if (list_empty(head)) { - return NULL; - } - - return head->next; -} - -element *list_insert_after(element *head, int data) { - element *new_element = (element *)malloc(sizeof(element)); - new_element->data = data; - - if (list_empty(head)) { - new_element->next = new_element; - } else { - new_element->next = head->next; - head->next = new_element; - } - - return new_element; -} - -element *list_insert_before(element *head, int data) { - if (list_empty(head)) { - return list_insert_after(head, data); - } - - element *iterator = head; - - while (iterator->next != head) { - iterator = iterator->next; - } - - return list_insert_after(iterator, data); -} - -element *list_search(element *head, int data) { - if (list_empty(head)) { - return NULL; - } - - element *iterator = head; - - do { - if (iterator->data == data) { - return iterator; - } - - iterator = iterator->next; - } while (iterator != head); - - return NULL; -} - -// This function cannot work when the element to be removed is the head. -element *list_remove(element *head, int data) { - element *el = list_search(head, data); - - if (el == NULL) { - return NULL; - } - - element *iterator = head; - - while (iterator->next != el) { - iterator = iterator->next; - } - - iterator->next = iterator->next->next; - return el; -} - -void list_free(element *head) { - if (list_empty(head)) { - return; - } - - element *iterator = head; - - do { - element *tmp = iterator; - iterator = iterator->next; - free(tmp); - } while (iterator != head); -} - -void list_process(element *head, int (*action)(int)) { - if (list_empty(head)) { - return; - } - - element *iterator = head; - - do { - iterator->data = action(iterator->data); - iterator = iterator->next; - } while (iterator != head); -} +#include "circ_list.h" + +#include <stdlib.h> + +element *list_create() { + return NULL; +} + +bool list_empty(element *head) { + return head == NULL; +} + +unsigned int list_count(element *head) { + if (list_empty(head)) { + return 0; + } + + unsigned int count = 0; + element *iterator = head; + + do { + count += 1; + iterator = iterator->next; + } while (iterator != head); + + return count; +} + +element *list_move(element *head) { + if (list_empty(head)) { + return NULL; + } + + return head->next; +} + +element *list_insert_after(element *head, int data) { + element *new_element = (element *)malloc(sizeof(element)); + new_element->data = data; + + if (list_empty(head)) { + new_element->next = new_element; + } else { + new_element->next = head->next; + head->next = new_element; + } + + return new_element; +} + +element *list_insert_before(element *head, int data) { + if (list_empty(head)) { + return list_insert_after(head, data); + } + + element *iterator = head; + + while (iterator->next != head) { + iterator = iterator->next; + } + + return list_insert_after(iterator, data); +} + +element *list_search(element *head, int data) { + if (list_empty(head)) { + return NULL; + } + + element *iterator = head; + + do { + if (iterator->data == data) { + return iterator; + } + + iterator = iterator->next; + } while (iterator != head); + + return NULL; +} + +element *list_remove(element **head, int data) { + element *el = list_search(*head, data); + + if (el == NULL) { + return NULL; + } + + element *iterator = *head; + + while (iterator->next != el) { + iterator = iterator->next; + } + + iterator->next = iterator->next->next; + + if (el == *head) { + *head = iterator; + } + + if (iterator == el) { + *head = NULL; + } + + return el; +} + +void list_free(element **head) { + if (list_empty(*head)) { + return; + } + + element *iterator = *head; + + do { + element *tmp = iterator; + iterator = iterator->next; + free(tmp); + } while (iterator != *head); + + *head = NULL; +} + +void list_process(element *head, int (*action)(int)) { + if (list_empty(head)) { + return; + } + + element *iterator = head; + + do { + iterator->data = action(iterator->data); + iterator = iterator->next; + } while (iterator != head); +} diff --git a/circ_list.h b/circ_list.h index bc90412dfc5a67325924ec2c2151e56db517e9a5..113c253d1c16a2eb680a0c0929ba5b747b139824 100644 --- a/circ_list.h +++ b/circ_list.h @@ -1,22 +1,22 @@ -#ifndef CIRC_LIST_H -#define CIRC_LIST_H - -#include <stdbool.h> - -typedef struct element { - int data; - struct element *next; -} element; - -element *list_create(); -bool list_empty(element *head); -unsigned int list_count(element *head); -element *list_move(element *head); -element *list_insert_after(element *head, int data); -element *list_insert_before(element *head, int data); -element *list_search(element *head, int data); -element *list_remove(element *head, int data); -void list_free(element *head); -void list_process(element *head, int (*action)(int)); - -#endif +#ifndef CIRC_LIST_H +#define CIRC_LIST_H + +#include <stdbool.h> + +typedef struct element { + int data; + struct element *next; +} element; + +element *list_create(); +bool list_empty(element *head); +unsigned int list_count(element *head); +element *list_move(element *head); +element *list_insert_after(element *head, int data); +element *list_insert_before(element *head, int data); +element *list_search(element *head, int data); +element *list_remove(element **head, int data); +void list_free(element **head); +void list_process(element *head, int (*action)(int)); + +#endif diff --git a/circ_list_tests.c b/circ_list_tests.c index a21bea61e9474c8a847a16a0d03cbcd87728cc09..20a362a85a815e35891ff0ed5a10bec4573402be 100644 --- a/circ_list_tests.c +++ b/circ_list_tests.c @@ -1,101 +1,102 @@ -#include <assert.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> - -#include "circ_list.h" - -int action_add_10(int element) { - return element + 10; -} - -void print_list(element *list) { - element *iterator = list; - - do { - printf("%d ", iterator->data); - iterator = iterator->next; - } while (iterator != list); - - printf("\n"); -} - -int main() { - // list_create - element *list = list_create(); - assert(list == NULL); - - // list_insert_after - printf("list_insert_after\n"); - list = list_insert_after(list, 1); - assert(list->data == 1); - print_list(list); - - list = list_insert_after(list, 3); - assert(list->data == 3); - assert(list->next->data == 1); - assert(list->next->next->data == 3); - print_list(list); - - list = list_insert_before(list, 2); - printf("list_insert_before\n"); - assert(list->data == 2); - assert(list->next->data == 3); - assert(list->next->next->data == 1); - assert(list->next->next->next->data == 2); - print_list(list); - - // list_empty - element *empty_list = list_create(); - assert(list_empty(empty_list) == true); - - assert(list_empty(list) == false); - - // list_count - assert(list_count(empty_list) == 0); - - assert(list_count(list) == 3); - - // list_search - element *search_result_1 = list_search(list, 1); - assert(search_result_1->data == 1); - - element *search_result_2 = list_search(list, 4); - assert(search_result_2 == NULL); - - // list_process - printf("list_process\n"); - list_process(list, action_add_10); - assert(list->data == 12); - assert(list->next->data == 13); - assert(list->next->next->data == 11); - assert(list->next->next->next->data == 12); - print_list(list); - - // list_remove - printf("list_remove\n"); - element *removed_element_1 = list_remove(list, 13); - assert(removed_element_1->data == 13); - assert(list->data == 12); - assert(list->next->data == 11); - assert(list->next->next->data == 12); - free(removed_element_1); - print_list(list); - - // list_move - printf("list_move\n"); - list = list_move(list); - assert(list->data == 11); - assert(list->next->data == 12); - assert(list->next->next->data == 11); - print_list(list); - - list = list_move(list); - assert(list->data == 12); - assert(list->next->data == 11); - assert(list->next->next->data == 12); - print_list(list); - - // list_free - list_free(list); -} +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#include "circ_list.h" + +int action_add_10(int element) { + return element + 10; +} + +void print_list(element *list) { + element *iterator = list; + + do { + printf("%d ", iterator->data); + iterator = iterator->next; + } while (iterator != list); + + printf("\n"); +} + +int main() { + // list_create + element *list = list_create(); + assert(list == NULL); + + // list_insert_after + printf("list_insert_after\n"); + list = list_insert_after(list, 1); + assert(list->data == 1); + print_list(list); + + list = list_insert_after(list, 3); + assert(list->data == 3); + assert(list->next->data == 1); + assert(list->next->next->data == 3); + print_list(list); + + list = list_insert_before(list, 2); + printf("list_insert_before\n"); + assert(list->data == 2); + assert(list->next->data == 3); + assert(list->next->next->data == 1); + assert(list->next->next->next->data == 2); + print_list(list); + + // list_empty + element *empty_list = list_create(); + assert(list_empty(empty_list) == true); + + assert(list_empty(list) == false); + + // list_count + assert(list_count(empty_list) == 0); + + assert(list_count(list) == 3); + + // list_search + element *search_result_1 = list_search(list, 1); + assert(search_result_1->data == 1); + + element *search_result_2 = list_search(list, 4); + assert(search_result_2 == NULL); + + // list_process + printf("list_process\n"); + list_process(list, action_add_10); + assert(list->data == 12); + assert(list->next->data == 13); + assert(list->next->next->data == 11); + assert(list->next->next->next->data == 12); + print_list(list); + + // list_remove + printf("list_remove\n"); + element *removed_element_1 = list_remove(&list, 13); + assert(removed_element_1->data == 13); + assert(list->data == 12); + assert(list->next->data == 11); + assert(list->next->next->data == 12); + free(removed_element_1); + print_list(list); + + // list_move + printf("list_move\n"); + list = list_move(list); + assert(list->data == 11); + assert(list->next->data == 12); + assert(list->next->next->data == 11); + print_list(list); + + list = list_move(list); + assert(list->data == 12); + assert(list->next->data == 11); + assert(list->next->next->data == 12); + print_list(list); + + // list_free + list_free(&list); + assert(list == NULL); +} diff --git a/joseph.c b/joseph.c index 5aee4ff533e5968587ced83a970a2aa79f2c9e66..5739915ce54c46d8f4be45483bb03ccaa3937490 100644 --- a/joseph.c +++ b/joseph.c @@ -9,20 +9,13 @@ element *add_number_to_circular_list(element *list, int number) { } element *remove_kth_element(element *head, int k) { - if (head->next == head) { - printf("%d\n", head->next->data); - free(head); - return NULL; - } - - for (int32_t i = 0; i < k - 1; i += 1) { + for (int32_t i = 0; i < k; i += 1) { head = list_move(head); } - printf("%d\n", head->next->data); - element *tmp = head->next; - head->next = head->next->next; - free(tmp); + element *removed_element = list_remove(&head, head->data); + printf("%d\n", removed_element->data); + free(removed_element); return head; }