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/listeNonTriees.c
2017-03-14 10:58:40 +01:00

86 lines
1.3 KiB
C

#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
// 2.1.1
struct Cellule {
struct Cellule* suiv;
int val;
};
typedef struct Cellule Cellule;
// 2.1.2
void ajout_tete(Cellule** l, int val) {
Cellule* p;
p = malloc(sizeof(Cellule));
p->val = val;
p->suiv = *l;
*l = p;
}
// 2.1.3
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);
}
// 2.1.4
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;
}
// 2.1.5
void supprimer_tete(Cellule** l) {
Cellule* p;
p = (*l)->suiv;
free(*l);
*l = p;
}
// 2.1.6
void desallouer(Cellule** l) {
if (l == NULL) {
return;
}
desallouer(&(*l)->suiv);
free(*l);
}
void desallouer2(Cellule** l) {
while ((*l)->suiv != NULL) {
supprimer_tete(l);
}
free(*l);
}
int main() {
// 2.1.7
Cellule* l = NULL;
ajout_tete(&l, 42);
ajout_tete(&l, 43);
ajout_tete(&l, 40);
imprimer(l);
printf("Est triée : %d\n", est_trie(l));
supprimer_tete(&l);
imprimer(l);
desallouer2(&l);
}