91 lines
1.4 KiB
C
91 lines
1.4 KiB
C
|
/* Implémentation d'une pile dynamique. */
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
struct maillon {
|
||
|
int val;
|
||
|
struct maillon *adr;
|
||
|
};
|
||
|
|
||
|
typedef struct maillon *Pile;
|
||
|
|
||
|
void CreerPile(Pile *pp) {
|
||
|
*pp = 0;
|
||
|
}
|
||
|
|
||
|
int PileVide(Pile p) {
|
||
|
return (p == 0);
|
||
|
}
|
||
|
|
||
|
int PilePleine(Pile p) {
|
||
|
return 0; // jamais pleine
|
||
|
}
|
||
|
|
||
|
int Empiler(int x, Pile *p) {
|
||
|
struct maillon *q;
|
||
|
|
||
|
if (PilePleine(*p)){
|
||
|
printf("La pile est pleine !\n");
|
||
|
return 1;
|
||
|
}
|
||
|
q = malloc(sizeof(*q));
|
||
|
q->val = x;
|
||
|
q->adr = *p;
|
||
|
*p = q;
|
||
|
|
||
|
printf("empilé %d\n", x);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int Depiler(int *x, Pile *p) {
|
||
|
struct maillon *q;
|
||
|
|
||
|
if (PileVide(*p)) {
|
||
|
printf("La pile est vide !\n");
|
||
|
return 1;
|
||
|
}
|
||
|
*x = (*p)->val;
|
||
|
q = *p;
|
||
|
*p = (*p)->adr;
|
||
|
free(q);
|
||
|
|
||
|
printf("valeur récupérée : %d\n", *x);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
int x = 0;
|
||
|
Pile p;
|
||
|
|
||
|
CreerPile(&p);
|
||
|
|
||
|
if (PileVide(p) == 1)
|
||
|
printf("La pile est vide.\n");
|
||
|
else
|
||
|
printf("La pile n'est pas vide.\n");
|
||
|
|
||
|
Empiler(4, &p);
|
||
|
Empiler(122, &p);
|
||
|
|
||
|
if (PileVide(p) == 1)
|
||
|
printf("La pile est vide.\n");
|
||
|
else
|
||
|
printf("La pile n'est pas vide.\n");
|
||
|
|
||
|
Depiler(&x, &p);
|
||
|
printf("x vaut %d\n", x);
|
||
|
|
||
|
if (PileVide(p) == 1)
|
||
|
printf("La pile est vide.\n");
|
||
|
else
|
||
|
printf("La pile n'est pas vide.\n");
|
||
|
|
||
|
Depiler(&x, &p);
|
||
|
printf("x vaut %d\n", x);
|
||
|
|
||
|
return 0;
|
||
|
}
|