From: Jérôme Benoit Date: Tue, 7 Mar 2017 11:03:47 +0000 (+0100) Subject: TP5: Implement iteratively searching an element in the linked list X-Git-Url: https://git.piment-noir.org/?p=Algorithmic_C.git;a=commitdiff_plain;h=ef5acd44fcca48173d91156aa07f9007efcd64e3 TP5: Implement iteratively searching an element in the linked list Signed-off-by: Jérôme Benoit --- diff --git a/TP5/exo4/liste_chainee.c b/TP5/exo4/liste_chainee.c index ddd82b1..b21ad04 100644 --- a/TP5/exo4/liste_chainee.c +++ b/TP5/exo4/liste_chainee.c @@ -3,6 +3,7 @@ /********************************************************************/ #include #include +#include typedef int element; @@ -74,6 +75,8 @@ void visualiser_iter(Liste L) void _visualiser_rec(Liste L, int compteur) { if (L != NULL) { + if (compteur == 0) + printf("--Debut--\n"); printf("L[%d]->value=%d\n", compteur, L->valeur); compteur++; _visualiser_rec(L->suivant, compteur); @@ -87,19 +90,23 @@ void visualiser_rec(Liste L) { int compteur = 0; - if (compteur == 0) - printf("--Debut--\n"); _visualiser_rec(L, compteur); } -int rechercher_iter(element e, Liste L) +bool rechercher_iter(element e, Liste L) { - /* ... */ + bool rt_val = false; + + while (L != NULL && L->valeur != e) { + L = L->suivant; + } + if (L->valeur == e) + rt_val = true; + return rt_val; } Liste rechercher_rec(element e, Liste L) { - /* ... */ } Liste ajouter_rec(element e, Liste L) @@ -141,6 +148,8 @@ int main() printf("L a pour longueur %d\n", longueur_rec(L)); visualiser_iter(L); visualiser_rec(L); + if (rechercher_iter(3, L)) + printf("Element 3 est present dans L\n"); /* ... */ }