Initiation à l algorithmique, L2 2005-2006, JC Fournier 3.1 Pro3 1 : listes chaînées Spécification : 1 generic 2 type element i s private ; 3 package l i s t e s c h a i n is 4 5 type position is private ; 6 type l i s t e is limited private ; 7 8 function est vide (L : l i s t e ) return boolean ; 9 function trouver (x : element ; L : l i s t e ) return position ; 10 function trouver prec (x : element ; L : l i s t e ) return position ; 11 function prem pos (L : l i s t e ) return position ; 12 function est der pos (p : position ; L : l i s t e ) return boolean ; 13 procedure avance pos (p : in out position ; L : l i s t e ) ; 14 function recup elt (p : position ; L : l i s t e ) return element ; 15 procedure inserer (x : element ; L : l i s t e ; p : position ) ; 16 procedure inserer debut (x : element ; L : l i s t e ) ; 17 procedure supprimer (x : element ; L : l i s t e ) ; 18 procedure c r e e r l i s t e (L : in out l i s t e ) ; 19 procedure v i d e r l i s t e (L : in out l i s t e ) ; 20 21 non trouve, f i n l i s t e : exception ; 22 23 private 24 type noeud ; 25 type position is access noeud ; 26 type noeud i s record 27 e l t : element ; 28 suiv : position ; 29 end record ; 30 type l i s t e is new position ; 31 32 end l i s t e s c h a i n ;
Initiation à l algorithmique, L2 2005-2006, JC Fournier 3.2 Corps : 1 with Ada. unchecked deallocation ; 2 package body l i s t e s c h a i n is 3 4 procedure l i b e r e r is 5 new Ada. unchecked deallocation (noeud, position ) ; 6 7 function est vide (L : l i s t e ) return boolean is 8 begin 9 return L. suiv = null ; 10 end est vide ; 11 12 function trouver (x : element ; L : l i s t e ) return position is 13 p : position := L. suiv ; 14 begin 15 while p /= null and then p. e l t /= x loop 16 p := p. suiv ; 17 end loop ; 18 if p = null then 19 raise non trouve ; 20 end if ; 21 return p ; 22 end trouver ; 23 24 function trouver prec (x : element ; L : l i s t e ) 25 return position is 26 p : position := position (L); 27 q : position ; 28 begin 29 while p /= null and then p. e l t /= x loop 30 q := p ; 31 p := p. suiv ; 32 end loop ; 33 if p = null then 34 raise non trouve ; 35 end if ; 36 return q ; 37 end trouver prec ;
Initiation à l algorithmique, L2 2005-2006, JC Fournier 3.3 38 39 function prem pos (L : l i s t e ) return position is 40 retourne null si la l i s t e est vide 41 begin 42 return L. suiv ; 43 end prem pos ; 44 45 function est der pos (p : position ; L : l i s t e ) 46 return boolean is 47 begin 48 return p. suiv = null ; 49 end est der pos ; 50 51 procedure avance pos (p : in out position ; L : l i s t e ) is 52 begin 53 if est der pos (p, L) then 54 raise f i n l i s t e ; 55 end if ; 56 p := p. suiv ; 57 end avance pos ; 58 59 function recup elt (p : position ; L : l i s t e ) return element is 60 begin 61 if p = null then 62 raise non trouve ; 63 else 64 return p. e l t ; 65 end if ; 66 end recup elt ; 67 68 procedure inserer (x : element ; L : l i s t e ; p : position ) is 69 begin 70 if p = null then 71 raise non trouve ; 72 else 73 p. suiv := new noeud ( x, p. suiv ) ; 74 end if ; 75 end inserer ; 76
Initiation à l algorithmique, L2 2005-2006, JC Fournier 3.4 77 procedure inserer debut (x : element ; L : l i s t e ) is 78 p : position ; 79 begin 80 p := new noeud ( x, L. suiv ) ; 81 L. suiv := p ; 82 end inserer debut ; 83 84 procedure supprimer (x : element ; L : l i s t e ) is 85 pos prec : position := trouver prec (x, L); 86 pos suppr : position := pos prec. suiv ; 87 begin 88 pos prec. suiv := pos suppr. suiv ; 89 l i b e r e r ( pos suppr ) ; libere l espace 90 end supprimer ; 91 92 procedure c r e e r l i s t e (L : in out l i s t e ) is 93 p : position ; 94 begin 95 p := new noeud ; 96 L := l i s t e (p ) ; 97 end c r e e r l i s t e ; 98 99 procedure v i d e r l i s t e (L : in out l i s t e ) is 100 p : position := L. suiv ; 101 temp : position ; 102 begin 103 L. suiv := null ; 104 while p /= null loop 105 temp := p. suiv ; 106 l i b e r e r (p ) ; 107 p := temp ; 108 end loop ; 109 end v i d e r l i s t e ; 110 111 end l i s t e s c h a i n ;
Initiation à l algorithmique, L2 2005-2006, JC Fournier 3.5 Essai : 1 with l i s t e s c h a i n ; 2 with Ada. text io, Ada. i n t e ger t e x t i o ; 3 use Ada. text io, Ada. i n teger text i o ; 4 5 procedure e s s a i l i s t e s c h a i n is 6 7 package l i s t e s c h a i n e n t is new l i s t e s c h a i n ( integer ) ; 8 use l i s t e s c h a i n e n t ; 9 10 L : l i s t e ; 11 p : position ; 12 x : integer ; 13 r : character ; 14 15 procedure aff menu is 16 begin 17 put ( MENU ) ; new line ; 18 put ( a : ajouter dans la l i s t e ) ; new line ; 19 put ( s : supprimer un element ) ; new line ; 20 put ( f : a f fi c h e r la l i s t e ) ; new line ; 21 put ( q : quitter ) ; new line ; 22 end aff menu ; 23 24 procedure ajout dans liste (L : l i s t e ) is 25 x : integer ; 26 begin 27 put ( Donner l entier a ajouter dans la l i s t e : ) ; 28 get (x ) ; 29 inserer debut (x, L); 30 end ajout dans liste ; 31 32 procedure suppr dans liste (L : l i s t e ) is 33 begin 34 put ( Donner l entier a supprimer de la l i s t e : ) ; 35 get (x ) ; 36 supprimer (x, L); 37 exception
Initiation à l algorithmique, L2 2005-2006, JC Fournier 3.6 38 when non trouve => 39 put ( impossible supprimer, element non dans la l i s t e ) ; 40 new line ; 41 end suppr dans liste ; 42 43 procedure a f f i c h l i s t e (L : l i s t e ) is 44 p : position ; 45 begin 46 if est vide (L) then 47 put ( l i s t e vide ) ; 48 else 49 p := prem pos (L); 50 loop 51 put ( recup elt (p, L ) ) ; 52 exit when est der pos (p, L); 53 avance pos (p, L) ; 54 end loop ; 55 put ( fin l i s t e ) ; 56 end if ; 57 new line ; 58 end a f f i c h l i s t e ; 59 60 begin 61 c r e e r l i s t e (L); 62 aff menu ; 63 loop 64 put ( choix : ) ; get ( r ) ; 65 case r is 66 when a => ajout dans liste (L); 67 when s => suppr dans liste (L); 68 when f => a f f i c h l i s t e (L); 69 when v => v i d e r l i s t e (L); 70 when q => put ( Au revoir ) ; new line ; exit ; 71 when others => put ( erreur de choix ) ; new line ; 72 end case ; 73 end loop ; 74 end e s s a i l i s t e s c h a i n ;