100 lines
1.8 KiB
C
100 lines
1.8 KiB
C
#include<stdlib.h>
|
||
#include<stdio.h>
|
||
#include<stdbool.h>
|
||
|
||
struct Cellule {
|
||
struct Cellule* suiv;
|
||
int val;
|
||
};
|
||
typedef struct Cellule Cellule;
|
||
|
||
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) {
|
||
while (l != NULL) {
|
||
printf("%d\n", l->val);
|
||
l = l->suiv;
|
||
}
|
||
}
|
||
|
||
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) {
|
||
ajout_tete(p, val);
|
||
return;
|
||
}
|
||
while (l->suiv != NULL && l->val < val) {
|
||
p = &l->suiv;
|
||
l = l->suiv;
|
||
}
|
||
if (l->val == val) {
|
||
return;
|
||
} else if (l->val < val) {
|
||
p = &l->suiv;
|
||
}
|
||
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 = NULL;
|
||
int val;
|
||
while (fscanf(fp, "%d", &val) != EOF) {
|
||
inserer(&l, val);
|
||
}
|
||
|
||
imprimer(l);
|
||
if (!est_trie(l)) {
|
||
printf("Erreur: La liste n'est pas correctement triée.\n");
|
||
return EXIT_FAILURE;
|
||
}
|
||
|
||
return EXIT_SUCCESS;
|
||
}
|