108 lines
1.9 KiB
C
108 lines
1.9 KiB
C
|
#include<stdlib.h>
|
|||
|
#include<stdio.h>
|
|||
|
#include<stdbool.h>
|
|||
|
|
|||
|
struct Cellule {
|
|||
|
struct Cellule* suiv;
|
|||
|
int val;
|
|||
|
};
|
|||
|
typedef struct Cellule Cellule;
|
|||
|
|
|||
|
Cellule* init_cellule(int val) {
|
|||
|
Cellule* l;
|
|||
|
l = malloc(sizeof(Cellule));
|
|||
|
l->suiv = NULL;
|
|||
|
l->val = val;
|
|||
|
return l;
|
|||
|
}
|
|||
|
|
|||
|
void ajout_tete(Cellule** l, int val) {
|
|||
|
Cellule* p;
|
|||
|
p = malloc(sizeof(Cellule));
|
|||
|
p->val = val;
|
|||
|
p->suiv = *l;
|
|||
|
*l = p;
|
|||
|
}
|
|||
|
|
|||
|
void imprimer(Cellule* l) {
|
|||
|
if (l == NULL) {
|
|||
|
return;
|
|||
|
}
|
|||
|
while (l->suiv != NULL) {
|
|||
|
printf("%d\n", l->val);
|
|||
|
l = l->suiv;
|
|||
|
}
|
|||
|
printf("%d\n", l->val);
|
|||
|
}
|
|||
|
|
|||
|
bool est_trie(Cellule* l) {
|
|||
|
if (l == NULL) {
|
|||
|
return true;
|
|||
|
}
|
|||
|
int temp;
|
|||
|
while (l->suiv != NULL) {
|
|||
|
temp = l->val;
|
|||
|
l = l->suiv;
|
|||
|
if (l->val < temp) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
void supprimer_tete(Cellule** l) {
|
|||
|
Cellule* p;
|
|||
|
p = (*l)->suiv;
|
|||
|
free(*l);
|
|||
|
*l = p;
|
|||
|
}
|
|||
|
|
|||
|
void desallouer(Cellule** l) {
|
|||
|
while ((*l)->suiv != NULL) {
|
|||
|
supprimer_tete(l);
|
|||
|
}
|
|||
|
free(*l);
|
|||
|
}
|
|||
|
|
|||
|
void inserer(Cellule** p, int val) {
|
|||
|
Cellule* l = *p;
|
|||
|
if (l == NULL) {
|
|||
|
l = init_cellule(val);
|
|||
|
return;
|
|||
|
}
|
|||
|
while (l->suiv != NULL && l->val < val) {
|
|||
|
p = &l->suiv;
|
|||
|
l = l->suiv;
|
|||
|
}
|
|||
|
if (l->val == val) {
|
|||
|
return;
|
|||
|
}
|
|||
|
if (l->suiv == NULL) {
|
|||
|
l->suiv = init_cellule(val);
|
|||
|
} else {
|
|||
|
ajout_tete(p, val);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int main(int argc, char* argv[]) {
|
|||
|
if (argc != 2) {
|
|||
|
printf("Usage : %s FICHIER_ENTREE\n", argv[0]);
|
|||
|
return EXIT_FAILURE;
|
|||
|
}
|
|||
|
|
|||
|
FILE* fp;
|
|||
|
fp = fopen(argv[1], "r");
|
|||
|
if (fp == NULL) {
|
|||
|
printf("Impossible d'ouvrir le fichier %s.\n", argv[1]);
|
|||
|
return EXIT_FAILURE;
|
|||
|
}
|
|||
|
|
|||
|
Cellule* l = init_cellule(42);
|
|||
|
int val;
|
|||
|
while (fscanf(fp, "%d", &val) != EOF) {
|
|||
|
inserer(&l, val);
|
|||
|
}
|
|||
|
|
|||
|
imprimer(l);
|
|||
|
}
|