typedef struct cell * liste_t;
typedef struct cell {
float obj;
struct cell * suiv;
} cell_t;
|
int estvide (liste_t l) {
if (l == (liste_t) 0) {
return 1;
}
return 0;
}
int estnonvide (liste_t l) {
if (l == (liste_t) 0) {
return 0;
}
return 1;
}
|
float soml (liste_t l) {
if (estvide(l)) {
return 0.0;
}
return (*l).obj + soml((*l).suiv);
}
|
int howmany (liste_t l) {
if (estvide(l)) {
return 0;
}
return 1 + howmany ((*l).suiv);
}
|
liste_t cons (int nb, liste_t l) {
liste_t new;
new = (liste_t) malloc (sizeof(cell_t));
assert(new);
(*new).nb = nb;
(*new).nxt = l;
return new;
}
liste_t creer (int nb) {
liste_t debut;
int i, j, k, t;
j = 13;
k = 21;
debut = (liste_t) 0;
for (i = 0; i < nb; i++) {
debut = cons (j, debut);
t = (j + k) % 31;
k = j;
j = t;
}
return debut;
}
|
void affl (liste_t l) {
if (estvide (l)) {
printf("\n");
}
else {
printf("%5.2f ", (*l).obj);
affl ((*l).suiv);
}
}
|
Notons que (*l).nxt s‘écrit aussi l->nxt, la flèche, ici "-" et ">", est un nouvel opérateur qui remplace une écriture un peu longue.
void affll (liste_t l) {
if (estvide (l)) {
printf("\n");
}
else {
printf("%5.2f ", l->obj);
affll (l->suiv);
}
}
void affw (liste_t l) {
while (estnonvide (l)) {
printf("%5.2f ", l->obj);
l = l->suiv;
}
printf("\n");
}
|
#include<stdio.h> #include<stdlib.h> #include<assert.h> |
int main () {
liste_t l;
int nb, s;
nb = 10;
l = creer(nb);
affw(l);
s = soml(l);
printf("Total : %d\n", s);
s = howmany(l);
printf("Il y a %d elements\n", s);
}
|