This repository has been archived on 2019-08-09. You can view files and clone it, but cannot push or open issues or pull requests.
s6-pa-tp/TP4/listeTrieesFichier.c
Geoffrey Frogeye b464da4e57 TP4 WIP
La fonction insérer n'est pas correcte
2017-03-10 18:16:55 +01:00

108 lines
1.9 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;
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);
}