Skip to content
Snippets Groups Projects
Verified Commit 0bf2e91e authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added pile in tableaux_1d

parent c541b50f
No related branches found
No related tags found
No related merge requests found
#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;
}
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment