diff --git a/slides/cours_11.md b/slides/cours_11.md index 229d409e51080021463b946171dfc171162b1690..2895e6dc659a1f4f1c28c406c1822b2074cd9e9c 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