From d88e5dbaeecb8b9134babfa816ba88a022c0f098 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Mon, 13 Dec 2021 14:41:49 +0100 Subject: [PATCH] recherche done --- slides/cours_11.md | 54 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/slides/cours_11.md b/slides/cours_11.md index 229d409..2895e6d 100644 --- a/slides/cours_11.md +++ b/slides/cours_11.md @@ -526,8 +526,23 @@ sorted_list sorted_list_push(sorted_list list, int val) { {width=70%} +. . . + +\footnotesize + ```C -// TODO add code +sorted_list sorted_list_extract(sorted_list list, int val) { + element *prec = *crt = list; // needed to glue elements together + while (NULL != crt && val > crt->data) { + prec = crt; + crt = crt->next; + } + if (NULL != crt && prec != crt && crt->data == val) { // glue things together + prec->next = crt->next; + free(crt); + } + return list; +} ``` @@ -537,14 +552,28 @@ sorted_list sorted_list_push(sorted_list list, int val) { . . . -{width=70%} +. . . + +\footnotesize + ```C -// TODO add code +sorted_list sorted_list_extract(sorted_list list, int val) { + element *prec = *crt = list; // needed to glue elements together + while (NULL != crt && val > crt->data) { + prec = crt; + crt = crt->next; + } + if (NULL != crt && crt->data == val && prec == crt) { // glue things together + list = list->next; + free(crt); + } + return list; +} ``` - # L'extraction 3. L'élément à extraire n'est **pas** dans la liste. @@ -556,6 +585,23 @@ premier.](figs/sorted_list_extract_first.svg){width=70%} On retourne la liste inchangée. +. . . + +\footnotesize + +```C +sorted_list sorted_list_extract(sorted_list list, int val) { + element *prec = *crt = list; // needed to glue elements together + while (NULL != crt && val > crt->data) { + prec = crt; + crt = crt->next; + } + if (NULL == crt || crt->data != val) { // val not present + return list; + } +} +``` + # La recherche # Exercice: recherche sans position -- GitLab