This repository has been archived on 2019-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
s6-pa-tp/TP4/listeTrieesFichier.c
2017-03-14 10:58:40 +01:00

100 lines
1.8 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}