diff --git a/circular_list/Makefile b/circular_list/Makefile index f53a5930aa92a8162c60a775494ee1b3076fb2b2..998fd2abf52278eaabbc58c2a8ba8a368155242c 100644 --- a/circular_list/Makefile +++ b/circular_list/Makefile @@ -2,6 +2,10 @@ cc=gcc CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak LFLAGS= -lm + +main: circ.x + ./circ.x + circ.x: circ.o main.o $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) diff --git a/circular_list/circ.c b/circular_list/circ.c index c6a16c2851a72189559e9be1fcc95ddb26308d9d..0ffdc4c38bf6dd27df974acc63db560588d649de 100644 --- a/circular_list/circ.c +++ b/circular_list/circ.c @@ -1,4 +1,5 @@ #include "circ.h" +#include <stdio.h> #include <stdlib.h> // typedef struct element { @@ -25,6 +26,7 @@ int list_count(element *head) { crt = crt->next; count++; } + printf("\n%d elements\n", count); return count; } @@ -35,19 +37,25 @@ element *list_move(element *head) { return head; } +element *move(element *head, int i) { + while (i > 0) { + head = list_move(head); + i--; + } + return head; +} + // Insert un élément après le pointeur head. Retourne le pointeur sur l’élé-ment // inséré element *list_insert_after(element *head, void *data) { if (list_empty(head)) { - printf("insert empty\n"); head = malloc(sizeof(element)); head->content = data; head->next = head; return head; } - printf("insert not empty\n"); element *tmp = malloc(sizeof(element)); tmp->content = data; tmp->next = head->next; @@ -58,9 +66,17 @@ element *list_insert_after(element *head, void *data) { // Insert un élément avant le pointeur head. Retourne le pointeur sur l’élé-ment // inséré element *list_insert_before(element *head, void *data) { + + if (list_empty(head)) { + head = malloc(sizeof(element)); + head->content = data; + head->next = head; + return head; + } + element *crt = head; while (crt->next != head) { - list_move(crt); + crt = crt->next; } element *tmp = malloc(sizeof(element)); tmp->content = data; @@ -91,29 +107,94 @@ element *list_search(element *head, void *data, // ouNULLsi l’élément est absent element *list_remove(element *head, void *data, int (*compare)(void *, void *)) { - element *tmp = list_search(head, data, compare); - if (tmp == NULL) { - return NULL; + + element *crt = head; + + while (compare(crt->next->content, data)) { + crt = crt->next; + if (crt->next == head) { + return NULL; + } } - element *removed = tmp->next; - tmp->next = tmp->next->next; + + element *removed = crt->next; + crt->next = crt->next->next; + return removed; } // Supprime toute la liste en libérant chaque élément void list_free(element *head, void (*data_free)(void *)) { - element *tmp = head; - while (!list_empty(tmp)) { - tmp = tmp->next; - data_free(head); + + element *tmp = head->next; + while (tmp != head) { + head->next = tmp->next; + data_free(tmp); + tmp = head->next; } + data_free(head); } // Appel d’une fonction de traitement quelconque sur chaque élément de laliste void list_process(element *head, void *(*action)(void *)) { - element *crt = head; - while (crt->next != head) { - action(crt->content); + element *crt = head->next; + while (crt != head) { + crt = action(crt); crt = crt->next; } + crt = action(head); +} + +void *print_int(void *x) { + printf("%d ", *(int *)((element *)x)->content); + return x; +} + +void free_simple(void *ptr) { free(ptr); } + +void custom_free(void *ptr) { + free(((element *)ptr)->content); + free(ptr); +} + +int cmp_int(void *a, void *b) { + + if (*(int *)a > *(int *)b) { + return 1; + } else if (*(int *)a < *(int *)b) { + return -1; + } else { + return 0; + } +} + +void joseph(element *head, int n, int k) { + + head = list_create(); + + for (int i = 0; i < n; i++) { + int *x = malloc(sizeof(int)); + *x = i + 1; + head = list_insert_after(head, x); + } + + for (int i = 0; i < n; i++) { + head = move(head, k - 1); + element *tmp = remove_crt(head); + printf("%d\n", *(int *)tmp->content); + custom_free(tmp); + } +} + +element *remove_crt(element *head) { + + element *crt = head->next; + while (crt != head) { + crt = crt->next; + } + + element *removed = crt->next; + crt->next = crt->next->next; + + return removed; } diff --git a/circular_list/circ.h b/circular_list/circ.h index 4ddb88979b8da23e6fb73393dffc83eeb5b58983..e8652179ae02524632d38914539de555cbc5bf20 100644 --- a/circular_list/circ.h +++ b/circular_list/circ.h @@ -22,6 +22,8 @@ int list_count(element* head); // Déplace le pointeur head sur l’élément suivant. Retourne la nouvelle têtede liste element* list_move(element* head); +element* move(element* head,int i); + // Insert un élément après le pointeur head. Retourne le pointeur sur l’élé-ment inséré element* list_insert_after(element* head, void* data); @@ -34,12 +36,15 @@ element* list_search(element* head,void* data,int (*compare)(void*, void*)); // Supprime un élément de la liste sans libérer l’élément pointé. Renvoie unpointeur sur le 1er élément trouvé à supprimer et l’enlève de la liste, ouNULLsi l’élément est absent element* list_remove(element* head, void* data,int (*compare)(void*, void*)); +element *remove_crt(element *head); + // Supprime toute la liste en libérant chaque élément void list_free(element* head, void (*data_free)(void*)); // Appel d’une fonction de traitement quelconque sur chaque élément de laliste void list_process(element* head, void* (*action)(void*)); +void joseph(element* head, int n,int k); diff --git a/circular_list/main.c b/circular_list/main.c index c31d2ed3f765ade0aea86ed22be7b333859e4970..3953b77379b19dfd715565e9e0bc662830bfe2e4 100644 --- a/circular_list/main.c +++ b/circular_list/main.c @@ -1,39 +1,13 @@ #include "circ.h" #include <stdlib.h> -void *print_int(void *x) { - printf("%d ", *(int *)x); - return x; -} -void free_simple(void *ptr) { free(ptr); } -int main() { - - element *head = list_create(); - - printf("%d\n", list_count(head)); - - int a = 1; - int b = 2; - int c = 3; - - head = list_insert_after(head, &a); - head = list_insert_after(head, &b); - head = list_insert_after(head, &a); - // list_insert_after(head, &b); - // list_insert_after(head, &c); - printf("%d\n", list_count(head)); - - // for (int i = 0; i < 8; i++) { - // list_insert_after(head, &i); - // } - - // list_process(head, print_int); - - // list_free(head, free_simple); +int main() { + element* head; + joseph(head, 8, 3); return EXIT_SUCCESS; } \ No newline at end of file