liste_double.c
2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "liste_double.h"
void init_liste_double (struct liste_double* L)
{
L->tete = (struct maillon_double*)0;
L->nbelem = 0;
}
void ajouter_en_tete_liste_double (struct liste_double* L, double d)
{ struct maillon_double* nouveau;
nouveau = (struct maillon_double*)malloc (sizeof (struct maillon_double));
assert (nouveau != (struct maillon_double*)0);
/* appeler ici un éventuel constructeur pour nouveau->value */
nouveau->value = d; /* affectation de la valeur */
nouveau->next = L->tete;
L->tete = nouveau;
L->nbelem += 1;
}
void clear_liste_double (struct liste_double* L)
{ struct maillon_double* courant;
struct maillon_double* suivant;
int i;
courant = L->tete;
for (i = 0; i < L->nbelem; i++)
{ suivant = courant->next;
/* appeler ici un éventuel destructeur pour nouveau->value */
free (courant);
courant = suivant;
}
}
/*
* Sous-fonction de set_liste_double.
* Si L = [m1, m2, ..., mn] avant l'appel alors
* L = [mn, ..., m2, m1] après l'appel.
* Aucune allocation dynamique n'est effectuée.
* Seuls les pointeurs sont modifiés
*/
static void retourner_liste_double (struct liste_double* L)
{ struct maillon_double *precedent, *courant, *suivant;
int i;
if (L->nbelem >= 2)
{ courant = L->tete;
suivant = courant->next;
courant->next = (struct maillon_double*)0;
for (i = 1; i < L->nbelem; i++)
{ precedent = courant;
courant = suivant;
suivant = suivant->next;
courant->next = precedent;
}
L->tete = courant;
}
}
void set_liste_double (struct liste_double* dst, struct liste_double* src)
{ struct maillon_double* M;
int i;
if (dst != src)
{ clear_liste_double (dst);
init_liste_double (dst);
M = src->tete;
for (i = 0; i < src->nbelem; i++)
{ ajouter_en_tete_liste_double (dst, M->value);
M = M->next;
}
retourner_liste_double (dst);
}
}
void extraire_tete_liste_double (double* d, struct liste_double* L)
{ struct maillon_double* tete;
assert (L->nbelem != 0);
tete = L->tete;
*d = tete->value; /* affectation */
L->tete = tete->next;
L->nbelem -= 1;
free (tete);
}
void imprimer_liste_double (struct liste_double* L)
{ struct maillon_double* M;
int i;
printf ("[");
M = L->tete;
for (i = 0; i < L->nbelem; i++)
{ if (i == 0)
printf ("%f", M->value);
else
printf (", %f", M->value);
M = M->next;
}
printf ("]\n");
}