diff --git a/source_codes/tableaux_1d/pile_array_int.c b/source_codes/tableaux_1d/pile_array_int.c new file mode 100644 index 0000000000000000000000000000000000000000..960ee6c7894f10e379dd994b9c24f67e4b02ddbe --- /dev/null +++ b/source_codes/tableaux_1d/pile_array_int.c @@ -0,0 +1,81 @@ +#include "pile_array_int.h" +#include <assert.h> +#include <limits.h> +#include <stdbool.h> +#include <stdlib.h> + +bool pile_est_valide(pile stack) { + return (stack.capacite > 0 && stack.sommet >= -1 && stack.data != NULL); +} + +bool pile_est_vide(pile stack) { + assert(pile_est_valide(stack)); + return (-1 == stack.sommet); +} + +bool pile_est_pleine(pile stack) { + assert(pile_est_valide(stack)); + return (stack.capacite - 1 == stack.sommet); +} + +int pile_count(pile stack) { + assert(pile_est_valide(stack)); + return stack.sommet + 1; +} + +int pile_sommet(pile stack) { + assert(!pile_est_vide(stack)); + return stack.data[stack.sommet]; +} + +pile pile_creer(int max) { + assert(max > 0); + pile stack; + stack.capacite = max; + stack.sommet = -1; + stack.data = malloc(max * sizeof(int)); + return stack; +} + +void pile_resize(pile *stack, int max) { + assert(pile_est_valide(*stack)); + if (max > stack->capacite) { + stack->capacite = max; + stack->data = realloc(stack->data, max * sizeof(int)); + } +} + +void pile_detruire(pile *stack) { + stack->capacite = -1; + stack->sommet = INT_MIN; + free(stack->data); + stack->data = NULL; +} + +void pile_empiler(pile *stack, int val) { + if (pile_est_pleine(*stack)) { + pile_resize(stack, stack->capacite + INCR); + } + stack->sommet++; + stack->data[stack->sommet] = val; +} + +int pile_depiler(pile *stack) { + int val = pile_sommet(*stack); + stack->sommet--; + return val; +} + +int main() { + pile p = pile_creer(10); + pile_empiler(&p, 1); + pile_empiler(&p, 10); + assert(pile_sommet(p) == 10); + assert(pile_depiler(&p) == 10); + assert(pile_depiler(&p) == 1); + assert(pile_est_vide(p)); + pile_detruire(&p); + + return EXIT_SUCCESS; +} + diff --git a/source_codes/tableaux_1d/pile_array_int.h b/source_codes/tableaux_1d/pile_array_int.h new file mode 100644 index 0000000000000000000000000000000000000000..101865dc2ff705e422ba30e56dc93b8df0ce85e1 --- /dev/null +++ b/source_codes/tableaux_1d/pile_array_int.h @@ -0,0 +1,31 @@ +#ifndef PILE_ARRAY_INT_H +#define PILE_ARRAY_INT_H + +#include <stdbool.h> +const int INCR = 100; + +typedef struct _pile { + int *data; + int sommet; + int capacite; +} pile; + +// Créer une nouvelle pile vide +pile pile_creer(int max); +// Libérer le tableau, mettre la capacité à < -1 +void pile_detruire(pile *stack); +// Empiler un élement au sommet de pile +void pile_empiler(pile *stack, int val); +// Dépiler un élément du sommet de la pile +int pile_depiler(pile *stack); +// Tester si la pile est vide +bool pile_est_vide(pile stack); +// Tester si la pile est pleine +bool pile_est_pleine(pile stack); +// Consulter l'élément au sommet de la pile +int pile_sommet(pile stack); +// Compter du nombre d'éléments de la pile: +int pile_count(pile stack); + +#endif +