(* Copyright (C) 2000-2001 Samuel Thibault This program is free software : you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation ; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with the program ; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. This License should be available in the same directory as this program, in a file called COPYING. If not, you may ask the webmaster or ftpmaster *) (* implémentation des listes doublement chainées *) type 'element liste2xchainée= Liste2xVide |Liste2x of ('element liste2xchainée)*'element*('element liste2xchainée);; (* Pour gueuler *) exception ExnListeVide;; (* Donne la valeur de l'élément en cours *) let valeur2x liste=match liste with Liste2xVide->raise ExnListeVide |Liste2x (_,n,_)->n;; (* Affiche les éléments qui sont en aval ( c'est-à-dire liste+ ) en utilisant print *) let rec print_2xsuite print liste=match liste with Liste2xVide->() |Liste2x (_,n,suite)->print n; print_string " ; "; print_2xsuite print suite;; (* Affiche les éléments qui sont en amont ( c'est-à-dire liste- ) en utilisant print *) let rec print_2xdébut print liste=match liste with Liste2xVide->() |Liste2x (début,n,_)->print_2xdébut print début; print n;print_string " ; ";; (* Affiche tous les éléments de la liste en utilisant print *) let print_2x print liste=match liste with Liste2xVide->print_string "{ ¤ }"; |Liste2x (l1,n,l2)->print_string "{ "; print_2xdébut print l1; print n; print_string " ; "; print_2xsuite print l2; print_string " ¤ }";; (* Spécialise les fonctions d'impression à quelques types standards *) let print_2xdébut_int=print_2xdébut print_int;; let print_2xsuite_int=print_2xsuite print_int;; let print_2x_int=print_2x print_int;; let print_2xdébut_string=print_2xdébut print_string;; let print_2xsuite_string=print_2xsuite print_string;; let print_2x_string=print_2x print_string;; let print_2xdébut_char=print_2xdébut print_char;; let print_2xsuite_char=print_2xsuite print_char;; let print_2x_char=print_2x print_char;; (* Donne le début, c'est-à-dire liste- *) let début2x liste=match liste with Liste2xVide->raise ExnListeVide |Liste2x (lg,n,_)->Liste2x(lg,n,Liste2xVide);; (* Donne la fin, c'est-à-dire liste+ *) let fin2x liste=match liste with Liste2xVide->raise ExnListeVide |Liste2x (_,n,ld)->Liste2x(Liste2xVide,n,ld);; (* Ajoute un élément à gauche de celui en cours, et reste sur l'élément en cours *) let ajoute2xg nb liste=match liste with Liste2xVide->Liste2x (Liste2xVide,nb,Liste2xVide) |Liste2x (lg,n,ld) -> let rec début=Liste2x(lg,nb,suite) and suite=Liste2x(début,n,ld) in suite;; (* Ajoute un élément à droite de celui en cours, et reste sur l'élément en cours *) let ajoute2xd nb liste=match liste with Liste2xVide->Liste2x (Liste2xVide,nb,Liste2xVide) |Liste2x (lg,n,ld) -> let rec début= Liste2x(lg,n,suite) and suite= Liste2x(début,nb,ld) in début;; (* Ajoute un élément à gauche de celui en cours, et se met sur le nouvel élément *) let ajoute2xgn nb liste=match liste with Liste2xVide->Liste2x (Liste2xVide,nb,Liste2xVide) |Liste2x (lg,n,ld) -> let rec début=Liste2x(lg,nb,suite) and suite=Liste2x(début,n,ld) in début;; (* Ajoute un élément à droite de celui en cours, et se met sur le nouvel élément *) let ajoute2xdn nb liste=match liste with Liste2xVide->Liste2x (Liste2xVide,nb,Liste2xVide) |Liste2x (lg,n,ld) -> let rec début= Liste2x(lg,n,suite) and suite= Liste2x(début,nb,ld) in suite;; (* Se déplace d'un rang vers la droite *) let droite2x liste=match liste with Liste2xVide->raise ExnListeVide |Liste2x (_,_,Liste2xVide)->raise ExnListeVide |Liste2x (_,_,Liste2x (lg,n,ld))->Liste2x (lg,n,ld);; (* Se déplace d'un rang vers la gauche *) let gauche2x liste=match liste with Liste2xVide->raise ExnListeVide |Liste2x (Liste2xVide,_,_)->raise ExnListeVide |Liste2x (Liste2x (lg,n,ld),_,_)->Liste2x (lg,n,ld);; (* Nom explicite... *) let rec liste2x_of_list liste=match liste with []->Liste2xVide |x::r->ajoute2xgn x (liste2x_of_list r);; (* petit test avec des chaînes de caractères *) let liste=ajoute2xg "Hey" (gauche2x (ajoute2xg "Bonjour !" (ajoute2xd "cool" (ajoute2xg "c'est vraiment" Liste2xVide))));; valeur2x liste;; print_2x_string liste;; print_2xdébut_string liste;; print_2xsuite_string liste;; (* un test de transtypage *) print_2x_int (liste2x_of_list (1::2::3::4::[]));;