PHP Classes

File: Ejemplo2.php

Recommend this page to a friend!
  Classes of Matías montes   List   Ejemplo2.php   Download  
File: Ejemplo2.php
Role: Example script
Content type: text/plain
Description: Example using objects and chained lists
Class: List
Manage double-linked lists of sorted elements
Author: By
Last change:
Date: 18 years ago
Size: 3,248 bytes
 

Contents

Class file image Download
<?php
/******************************************************************************
 * BBBB AAA RRRR BBBB AAA ZZZZZZ U U L *
 * B B A A R R B B A A ZZ U U L *
 * B B A A R R B B A A ZZ U U L *
 * BBBB A A RRRR BBBB A A Z U U L *
 * B B AAAAAAA R R B B AAAAAAA Z U U L *
 * B B A A R R B B A A ZZ U U L *
 * B B A A R R B B A A ZZ U U L *
 * BBBBB A A R R BBBBB A A ZZZZZZ UUU LLLLLLL *
 ******************************************************************************/

/* Ejemplo de utilización de Lista Genérica en múltiples niveles
 * Descripción: En este ejemplo se muestra el uso de una lista encadenada
 * utilizando datos de tipo Objeto.
 * Autor: Matías Montes
 * Versión : 2.0
 */

/******************************************************************************/
/* Librerías y definiciones requeridas */
/*-------------------------------------*/

require_once("class.Lista.php");

if( !
defined("MAX") ) define("MAX", 10);


class
Nivel1
{
    var
$clave;
    var
$sublista;

    function
Nivel1($clave)
    {
       
$this->clave = $clave;
       
$this->sublista = new Lista("CompararNivel2");
    }
}

function
CompararNivel1($DatoEnteroIzq , $DatoEnteroDer)
{
  
$resultado = IGUAL;

   if ( (
$DatoEnteroIzq->clave) == ($DatoEnteroDer->clave) ) $resultado = IGUAL;
   if ( (
$DatoEnteroIzq->clave) > ($DatoEnteroDer->clave) ) $resultado = MAYOR;
   if ( (
$DatoEnteroIzq->clave) < ($DatoEnteroDer->clave) ) $resultado = MENOR;

   return
resultado;
}

function
CompararNivel2($DatoEnteroIzq , $DatoEnteroDer)
{

  
$resultado = IGUAL;

   if ( (
$DatoEnteroIzq) == ($DatoEnteroDer) ) $resultado = IGUAL;
   if ( (
$DatoEnteroIzq) > ($DatoEnteroDer) ) $resultado = MAYOR;
   if ( (
$DatoEnteroIzq) < ($DatoEnteroDer) ) $resultado = MENOR;

   return
$resultado;
}

/******************************************************************************/
/* Programa Principal */
/*--------------------*/

//Instancio la lista de Nivel 1
$L = new Lista("CompararNivel1");

//Cargo la lista en 2 niveles
for( $i=0 ; $i<MAX ; $i++ )
{
   
$datoN1 = new Nivel1($i);
    for(
$j=1 ; $j<MAX ; $j++ )
    {
       
$datoN2 = $j + $i * MAX;
       
$datoN1->sublista->insertarDato($datoN2);
    }
   
$L->insertarDato($datoN1);
}

//Imprime el contenido de la lista
$cursor1 = $L->primero();

echo
"<UL>\n";
while(
$cursor1!=$L->fin() )
{
   
$datoN1 = $L->getDato($cursor1);
    echo
" <LI>".$datoN1->clave."\n";
    echo
" <UL>\n";

   
$cursor2 = $datoN1->sublista->primero();
    while(
$cursor2!=$datoN1->sublista->fin() )
    {
       
$datoN2 = $datoN1->sublista->getDato($cursor2);
        echo
" <LI>".$datoN2."</LI> \n";
       
$cursor2 =& $datoN1->sublista->siguiente($cursor2);
    }

    echo
" </UL>\n";
    echo
" </LI>\n";

   
$cursor1 =& $L->siguiente($cursor1);
}
echo
"</UL>\n";

//Destruyo la lista
$L->destruir();
?>