From 105fac432fc6e4e99fef2a3d584fe20576d6745c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 7 Mar 2017 10:58:29 +0100 Subject: [PATCH] TP5: Implement linked list counting iteratively and recursively MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP5/exo4/liste_chainee.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/TP5/exo4/liste_chainee.c b/TP5/exo4/liste_chainee.c index 7d55081..4b93fc2 100644 --- a/TP5/exo4/liste_chainee.c +++ b/TP5/exo4/liste_chainee.c @@ -42,8 +42,7 @@ int longueur_iter(Liste L) { int longueur = 0; - while (L->suivant != NULL) - { + while (L != NULL) { L = L->suivant; longueur++; } @@ -52,7 +51,11 @@ int longueur_iter(Liste L) int longueur_rec(Liste L) { - /* ... */ + if (L != NULL) { + return 1 + longueur_rec(L->suivant); + } else { + return 0; + } } void visualiser_iter(Liste L) @@ -105,9 +108,13 @@ int main() { int x; Liste L = NULL; + L = ajouter_iter(2, L); + L = ajouter_iter(1, L); + L = ajouter_iter(3, L); + printf("Saisir un entier a ajouter a la site\n"); scanf("%d", &x); L = ajouter_iter(x, L); - printf("longueur=%d\n", longueur_iter(L)); + printf("longueur=%d\n", longueur_rec(L)); visualiser_iter(L); /* ... */ } -- 2.34.1