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

joseph

parent 389b7d40
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
#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) {
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;
}
......@@ -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);
......
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment