2021-09-07 15:43:01 +02:00
|
|
|
/* Implémentation d'une file dynamique. */
|
|
|
|
|
2021-09-06 16:06:52 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
struct maillon {
|
|
|
|
int val;
|
|
|
|
struct maillon *adr;
|
2021-09-06 16:06:52 +02:00
|
|
|
};
|
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
typedef struct {
|
|
|
|
struct maillon *tete, *queue;
|
|
|
|
} File;
|
2021-09-06 16:06:52 +02:00
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
void CreerFile(File *pf) {
|
|
|
|
pf->tete = NULL;
|
|
|
|
pf->queue = NULL;
|
2021-09-06 16:06:52 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
int FileVide(File f) {
|
|
|
|
return (f.tete == 0);
|
|
|
|
}
|
2021-09-06 16:06:52 +02:00
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
int FilePleine(File f) {
|
|
|
|
return 0; // jamais pleine
|
2021-09-06 16:06:52 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
int Enfiler(int x, File *pf) {
|
|
|
|
struct maillon *p;
|
|
|
|
if (FilePleine(*pf)) return 1;
|
2021-09-06 16:06:52 +02:00
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
p = malloc(sizeof(*p));
|
|
|
|
if (p == 0) return 1;
|
2021-09-06 16:06:52 +02:00
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
p->val = x; p->adr = 0;
|
|
|
|
if (pf->queue) pf->queue->adr = p;
|
|
|
|
else pf->tete = p;
|
|
|
|
|
|
|
|
pf->queue = p;
|
2021-09-06 16:06:52 +02:00
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
return 0;
|
2021-09-06 16:06:52 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 15:43:01 +02:00
|
|
|
int Defiler(int *x, File *pf) {
|
|
|
|
struct maillon *p;
|
|
|
|
if (FileVide(*pf)) return 1;
|
|
|
|
|
|
|
|
*x = pf->tete->val;
|
|
|
|
p = pf->tete;
|
|
|
|
pf->tete = pf->tete->adr;
|
|
|
|
free(p); p = 0;
|
|
|
|
if (pf->tete == 0) pf->queue = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
int main() {
|
2021-09-07 15:43:01 +02:00
|
|
|
int x = 0;
|
2021-09-07 13:43:20 +02:00
|
|
|
File f;
|
|
|
|
|
|
|
|
CreerFile(&f);
|
|
|
|
|
|
|
|
if (FileVide(f) == 1)
|
|
|
|
printf("La file est vide.\n");
|
|
|
|
else
|
|
|
|
printf("La file n'est pas vide.\n");
|
2021-09-06 16:06:52 +02:00
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
Enfiler(4, &f);
|
|
|
|
|
2021-09-07 15:43:01 +02:00
|
|
|
if (FileVide(f) == 1)
|
|
|
|
printf("La file est vide.\n");
|
|
|
|
else
|
|
|
|
printf("La file n'est pas vide.\n");
|
|
|
|
|
|
|
|
Defiler(&x, &f);
|
|
|
|
printf("x vaut %d\n", x);
|
|
|
|
|
2021-09-07 13:43:20 +02:00
|
|
|
if (FileVide(f) == 1)
|
|
|
|
printf("La file est vide.\n");
|
|
|
|
else
|
|
|
|
printf("La file n'est pas vide.\n");
|
|
|
|
|
|
|
|
return 0;
|
2021-09-06 16:06:52 +02:00
|
|
|
}
|