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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| with Es_Simples; use Es_Simples;
with ADA.IO_EXCEPTIONS;
package body P_liste_gen is
function Est_Vide(L : T_Liste_Gen) return Boolean is
begin
return L = null;
end Est_Vide;
--****************************************
-- Ajout en tete
--****************************************
function Ajout_Tete (L : in T_Liste_Gen ; N : in T) return T_Liste_Gen is
begin
return new Doublet'(Val=>N,Suiv=>L);
end Ajout_Tete;
--****************************************
-- Creation de la liste
--****************************************
function Creation_Liste return T_Liste_Gen is
L : T_Liste_Gen:=null;
Val : T;
Saisie_terminee:exception;
Val2 : character;
stop : boolean :=false;
begin
while not stop loop
-- begin
begin
Put_Line ("Veuillez saisir une valeur");
Get(Val);
L:=Ajout_Tete(L,Val);
loop
Put_Line("Désirez vous continuer ? O/N");
Get(Val2);
if Val2='N' then stop:=true; end if;
exit when Val2='N' or Val2='O';
Put_Line("Veuillez saisir O ou N en fonction de votre souhait");
end loop;
exception
when ADA.IO_EXCEPTIONS.DATA_ERROR => Put ("Erreur de saisie, type de données saisi incorrect : ");Skip_Line;
end;
end loop;
Put_Line("Saisie terminée");
return L;
end Creation_Liste;
--****************************************
-- procedures d'affichage
--****************************************
procedure Affichage_Classique (L : in T_Liste_Gen) is
L2 : T_Liste_Gen :=L;
begin
While not Est_Vide (L2) loop
Put (' ');Put(L2.Val);
L2:=L2.Suiv;
end loop;
Put_Line("Fin de l'affichage");
New_Line;
end Affichage_Classique;
procedure Affichage_Joli (L : in T_Liste_Gen) is
L2:T_Liste_Gen :=L;
Nb_Elem : Integer:=0;
begin
while not Est_Vide(L2) loop
Nb_Elem:=Nb_Elem+1;
L2:=L2.suiv;
end loop;
for I in 1..Nb_Elem loop
Ecrire_Ligne1;
end loop;
New_Line;
L2:=L;
while not Est_Vide(L2) loop
Ecrire_Ligne2(L2);
L2:=L2.Suiv;
end loop;
Put("NULL");
New_Line;
for I in 1..Nb_Elem loop
Ecrire_Ligne1;
end loop;
New_Line;
end Affichage_Joli;
--****************************************
-- fonctions necessaires a Affichage_Joli
--****************************************
procedure Ecrire_Ligne1 is
begin
Put("################## ");
end Ecrire_Ligne1;
procedure Ecrire_Ligne2(L:T_Liste_Gen) is
begin
Put('#');
Put(L.Val);
Put(" # --#-> ");
end Ecrire_Ligne2;
--***************************************
--****************************************
-- procedures d'ajout en fin de liste
--****************************************
procedure Ajout_Fin (L : in out T_Liste_Gen ; N : in T) is
begin
if Est_Vide(L) then
L:=Ajout_Tete(null,N);
else
Ajout_Fin (L.Suiv,N);
end if;
end Ajout_Fin;
procedure Ajout_Fin_Copie (L : in T_Liste_Gen ; L2 : out T_Liste_Gen ; N : in T) is
P : T_Liste_Gen:=L;
P2,P3 : T_Liste_Gen;
begin
if Est_Vide(P) then
L2 := Ajout_Tete(null,N);
else
P2:=Ajout_Tete(null,P.val);
P3:=P2;
while not Est_Vide(P.suiv) loop
P:=P.suiv;
P2.suiv:=Ajout_Tete(null,P.val);
P2:=P2.suiv;
end loop;
P2.suiv:=Ajout_tete(null,N);
L2:=P3;
end if;
end Ajout_Fin_Copie;
--****************************************
-- procedures d'inversion de l'ordre de la liste
--****************************************
procedure Inversion_Iterative (L : in out T_Liste_Gen ) is
P:T_Liste_Gen:=L;
Suivant : T_Liste_Gen;
Precedent: T_Liste_Gen;
begin
while not Est_Vide(P) loop
Suivant:=P.suiv;
P.suiv:=Precedent;
Precedent:=P;
P:=Suivant;
end loop;
L:=Precedent;
end Inversion_Iterative;
procedure Inversion_Recursive (L : in out T_Liste_Gen;P : in T_Liste_Gen ) is
Save : T_Liste_Gen;
begin
if not Est_Vide(L) then
Inversion_Recursive(L.Suiv, L);
Save:=L.suiv;
L.Suiv:=P;
L:=Save;
else
L:=P;
end if;
end Inversion_Recursive;
--****************************************
-- procedures de recherche de sous liste
--****************************************
procedure Recherche_Sous_Liste_Proc (L1 : in T_Liste_Gen ;L2 : T_Liste_Gen; Ok : out Boolean) is
PP1: T_Liste_Gen :=L1;
P1 : T_Liste_Gen :=L1;
P2 : T_Liste_Gen :=L2;
begin
ok:=false;
while (not Est_Vide(P1)) and (not Est_Vide(P2)) loop
if P1.Val = P2.Val then
P1:=P1.Suiv;
P2:=P2.Suiv;
else
PP1:=PP1.Suiv;
P1:=PP1;
P2:=L2;
end if;
end loop;
Ok := Est_Vide(P2);
end Recherche_Sous_Liste_Proc;
function Recherche_Sous_Liste_Fonc (L1, L2 : in T_Liste_Gen) return boolean is
function Est_Au_Debut (L1, L2 : T_Liste_Gen) return boolean is
-- renvoie vrai si L2 est au debut de L1
begin
return (L2 = null) or else ((L1 /= null) and then (L1.val = L2.val) and then Est_Au_Debut(L1.suiv, L2.suiv));
end Est_Au_Debut;
begin
return Est_Au_Debut(L1,L2) or else ((L1 /= null) and then Recherche_Sous_Liste_Fonc(L1.suiv,L2));
end Recherche_Sous_Liste_Fonc;
end P_liste_gen; |
Partager