diff --git a/ex3/ex3.c b/ex3/ex3.c index df55467f72fe89c8ad551477fb68137fe08b98aa..dfacbcbaacaa2d2c358d7f13db10f2538ca2665f 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -34,50 +34,6 @@ void list_push(list* ll, const int v) { ll->head = e; } -void list_insert(list* ll, const int n, const int v) { - if (n < 0) { return; } - elem *e = malloc(sizeof(elem)); - e->value = v; - e->next = NULL; - if (0 == n) { - e->next = ll->head; - ll->head = e; - } else { - int count = 1; - elem *prev = ll->head; - while (count < n) { - if (NULL == prev->next) { return; } - prev = prev->next; - ++count; - } - elem *follow = prev->next; - e->next = follow; - prev->next = e; - } -} - -void list_remove(list *ll, const int n) { - if (n < 0) { return; } - if (0 == n) { - elem *e = ll->head; - ll->head = ll->head->next; - free(e); - } else { - int count = 1; - elem *prev = ll->head; - elem *e = prev->next; - while (count < n) { - if (NULL == e) { return; } - prev = e; - e = e->next; - ++count; - } - if (e != NULL) { prev->next = e->next; } - else { prev->next = NULL; } - free(e); - } -} - void list_print(const list *ll) { elem *e = ll->head; while (e != NULL) { @@ -94,19 +50,29 @@ void list_print(const list *ll) { list pascal(const int line) { if (line <= 1) { list ll = list_create(); - ll.push(1); + list_push(&ll, 1); return ll; } else { - list above = pascal(line-1); // rec - elem *e = above.head; - list_ll = list_create(); - list_destroy(&above); + list prevline = pascal(line-1); // rec + // 2l: on "triche" et on utilise la symmétrie du triangle + list ll = list_create(); + list_push(&ll, 1); + elem *above = prevline.head; + elem *northwest = prevline.head->next; + while (northwest != NULL) { + // même trick + list_push(&ll, above->value + northwest->value); + above = northwest; + northwest = northwest->next; + } + list_push(&ll, 1); + list_destroy(&prevline); return ll; } } -int main(int argc, char **argv) { +int main() { int line; scanf("%d", &line); printf("\n");