diff --git a/Makefile b/Makefile index aa3a4df74d5eb5610e517db4e5eeb4e88892153b..61f18ed30ee1cf99bfab06598c5f88241ced9773 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,23 @@ -CC = gcc -CCFLAGS = -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak - -circ_list.o: circ_list.c circ_list.h - $(CC) $(CCFLAGS) -c $< -o $@ - -joseph.o: joseph.c - $(CC) $(CCFLAGS) -c $< -o $@ - -joseph: joseph.o circ_list.o - $(CC) $(CCFLAGS) -o joseph joseph.o circ_list.o - -pointer_sort: pointer_sort.o - $(CC) $(CCFLAGS) -o pointer_sort pointer_sort.o - -circ_list_tests.o: circ_list_tests.c - $(CC) $(CCFLAGS) -c $< -o $@ - -circ_list_tests: circ_list_tests.o circ_list.o - $(CC) $(CCFLAGS) -o circ_list_tests circ_list_tests.o circ_list.o - -clean: - rm -f *.o joseph pointer_sort circ_list_tests +CC = gcc +CCFLAGS = -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + +circ_list.o: circ_list.c circ_list.h + $(CC) $(CCFLAGS) -c $< -o $@ + +joseph.o: joseph.c + $(CC) $(CCFLAGS) -c $< -o $@ + +joseph: joseph.o circ_list.o + $(CC) $(CCFLAGS) -o joseph joseph.o circ_list.o + +pointer_sort: pointer_sort.o + $(CC) $(CCFLAGS) -o pointer_sort pointer_sort.o + +circ_list_tests.o: circ_list_tests.c + $(CC) $(CCFLAGS) -c $< -o $@ + +circ_list_tests: circ_list_tests.o circ_list.o + $(CC) $(CCFLAGS) -o circ_list_tests circ_list_tests.o circ_list.o + +clean: + rm -f *.o joseph pointer_sort circ_list_tests diff --git a/circ_list.c b/circ_list.c index 93c2c3405a0bb7953d00ba975e722292b453ed32..adf0841e8e0193dae81b1dcbf60678ca532562a6 100644 --- a/circ_list.c +++ b/circ_list.c @@ -1,126 +1,126 @@ -#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; +} + +// 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); +} diff --git a/circ_list.h b/circ_list.h index 278e7ebbdf5623af4c242c23bc01f5ad9091bd8d..bc90412dfc5a67325924ec2c2151e56db517e9a5 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 3cdc78bc96032b089b2934c7d7bf72e150913db8..a21bea61e9474c8a847a16a0d03cbcd87728cc09 100644 --- a/circ_list_tests.c +++ b/circ_list_tests.c @@ -1,101 +1,101 @@ -#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); +}