Skip to content
Snippets Groups Projects
Commit 60cdc454 authored by thib's avatar thib
Browse files

joseph

parent 389b7d40
Branches
No related tags found
No related merge requests found
...@@ -2,6 +2,10 @@ cc=gcc ...@@ -2,6 +2,10 @@ cc=gcc
CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak
LFLAGS= -lm LFLAGS= -lm
main: circ.x
./circ.x
circ.x: circ.o main.o circ.x: circ.o main.o
$(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS)
......
#include "circ.h" #include "circ.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// typedef struct element { // typedef struct element {
...@@ -25,6 +26,7 @@ int list_count(element *head) { ...@@ -25,6 +26,7 @@ int list_count(element *head) {
crt = crt->next; crt = crt->next;
count++; count++;
} }
printf("\n%d elements\n", count);
return count; return count;
} }
...@@ -35,19 +37,25 @@ element *list_move(element *head) { ...@@ -35,19 +37,25 @@ element *list_move(element *head) {
return 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 // Insert un élément après le pointeur head. Retourne le pointeur sur l’élé-ment
// inséré // inséré
element *list_insert_after(element *head, void *data) { element *list_insert_after(element *head, void *data) {
if (list_empty(head)) { if (list_empty(head)) {
printf("insert empty\n");
head = malloc(sizeof(element)); head = malloc(sizeof(element));
head->content = data; head->content = data;
head->next = head; head->next = head;
return head; return head;
} }
printf("insert not empty\n");
element *tmp = malloc(sizeof(element)); element *tmp = malloc(sizeof(element));
tmp->content = data; tmp->content = data;
tmp->next = head->next; tmp->next = head->next;
...@@ -58,9 +66,17 @@ element *list_insert_after(element *head, void *data) { ...@@ -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 // Insert un élément avant le pointeur head. Retourne le pointeur sur l’élé-ment
// inséré // inséré
element *list_insert_before(element *head, void *data) { 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; element *crt = head;
while (crt->next != head) { while (crt->next != head) {
list_move(crt); crt = crt->next;
} }
element *tmp = malloc(sizeof(element)); element *tmp = malloc(sizeof(element));
tmp->content = data; tmp->content = data;
...@@ -91,29 +107,94 @@ element *list_search(element *head, void *data, ...@@ -91,29 +107,94 @@ element *list_search(element *head, void *data,
// ouNULLsi l’élément est absent // ouNULLsi l’élément est absent
element *list_remove(element *head, void *data, element *list_remove(element *head, void *data,
int (*compare)(void *, void *)) { int (*compare)(void *, void *)) {
element *tmp = list_search(head, data, compare);
if (tmp == NULL) { element *crt = head;
return NULL;
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; return removed;
} }
// Supprime toute la liste en libérant chaque élément // Supprime toute la liste en libérant chaque élément
void list_free(element *head, void (*data_free)(void *)) { void list_free(element *head, void (*data_free)(void *)) {
element *tmp = head;
while (!list_empty(tmp)) { element *tmp = head->next;
tmp = tmp->next; while (tmp != head) {
data_free(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 // Appel d’une fonction de traitement quelconque sur chaque élément de laliste
void list_process(element *head, void *(*action)(void *)) { void list_process(element *head, void *(*action)(void *)) {
element *crt = head; element *crt = head->next;
while (crt->next != head) { while (crt != head) {
action(crt->content); crt = action(crt);
crt = crt->next; 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;
} }
...@@ -22,6 +22,8 @@ int list_count(element* head); ...@@ -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 // Déplace le pointeur head sur l’élément suivant. Retourne la nouvelle têtede liste
element* list_move(element* head); 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é // 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); element* list_insert_after(element* head, void* data);
...@@ -34,12 +36,15 @@ element* list_search(element* head,void* data,int (*compare)(void*, void*)); ...@@ -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 // 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* 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 // Supprime toute la liste en libérant chaque élément
void list_free(element* head, void (*data_free)(void*)); void list_free(element* head, void (*data_free)(void*));
// Appel d’une fonction de traitement quelconque sur chaque élément de laliste // Appel d’une fonction de traitement quelconque sur chaque élément de laliste
void list_process(element* head, void* (*action)(void*)); void list_process(element* head, void* (*action)(void*));
void joseph(element* head, int n,int k);
......
#include "circ.h" #include "circ.h"
#include <stdlib.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)); int main() {
element* head;
// for (int i = 0; i < 8; i++) { joseph(head, 8, 3);
// list_insert_after(head, &i);
// }
// list_process(head, print_int);
// list_free(head, free_simple);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment