From 30f00d78a1e7daf2cba75674c230c1487c7a8044 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Sun, 12 Dec 2021 23:19:22 +0100 Subject: [PATCH] added insert for sorted list --- slides/cours_11.md | 125 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 5 deletions(-) diff --git a/slides/cours_11.md b/slides/cours_11.md index a1bb1ac..761c7f8 100644 --- a/slides/cours_11.md +++ b/slides/cours_11.md @@ -47,8 +47,7 @@ patat: * Entre les deux, les éléments sont stockés dans une liste chaînée (comme une pile). - + ## Structure de données en C? @@ -269,7 +268,7 @@ typedef struct _queue { # Complexité -## Quelle sont les complexité de: +## Quelle sont les complexités de: * Initialisation? @@ -336,6 +335,52 @@ typedef struct _queue { } queue; ``` +# Une file plus efficace (implémentation) + +## Enfilage + +```C +void queue_enqueue(queue *fa, int val) { + if ((fa->head == 0 && fa->tail == fa->capacity-1) || + (fa->tail == (fa->head-1) % (fa->capacity-1))) { + return; // queue is full + } + if (fa->head == -1) { // queue was empty + fa->head = fa->tail = 0; + fa->data[fa->tail] = val; + } else if (fa->tail == fa->capacity-1 && fa->head != 0) { + // the tail reached the end of the array + fa->tail = 0; + fa->data[fa->tail] = val; + } else { + // nothing particular + fa->tail += 1; + fa->data[fa->tail] = val; + } +} +``` + +# Une file plus efficace (implémentation) + +## Défilage + +```C +// Function to delete element from Circular Queue +void queue_dequeue(queue *fa, int *val) { + if (queue_is_empty(*fa)) { + return; // queue is empty + } + *val = fa->data[fa->head]; + if (fa->head == fa->tail) { // that was the last element + fa->head = fa->tail = -1; + } else if (fa->head == fa->capacity-1) { + fa->head = 0; + } else { + fa->head += 1; + } +} +``` + # File basée sur un tableau (git) @@ -351,8 +396,7 @@ typedef struct _queue { Une liste chaînée triée est: * une liste chaînée -* dont les éléments sont insérés dans -l'ordre. +* dont les éléments sont insérés dans l'ordre.  @@ -400,6 +444,77 @@ element* sorted_list_search(sorted_list list, int val); # L'insertion +## Trois cas + +1. La liste est vide. + +. . . + +{width=30%} + +. . . + +```C +sorted_list sorted_list_push(sorted_list list, int val) { + if (sorted_list_is_empty(list)) { + list = malloc(sizeof(*list)); + list->data = val; + list->next = NULL; + return list; + } +} +``` + +# L'insertion + +2. L'insertion se fait en première position. + +. . . + +{width=80%} + +. . . + +```C +sorted_list sorted_list_push(sorted_list list, int val) { + if (list->data >= val) { + element *tmp = malloc(sizeof(*tmp)); + tmp->data = val; + tmp->next = list; + list = tmp; + return list; + } +} +``` + +# L'insertion + +3. L'insertion se fait sur une autre position que la première. + +. . . + +{width=70%} + +. . . + +\footnotesize + +```C +sorted_list sorted_list_push(sorted_list list, int val) { + element *tmp = malloc(sizeof(*tmp)); + tmp->data = val; + element *crt = list; + while (NULL != crt->next && val > crt->next->data) { + crt = crt->next; + } + tmp->next = crt->next; + crt->next = tmp; + return list; +} +``` + # L'extraction # La recherche -- GitLab