Login   Register  
PHP Classes
elePHPant
Icontem

File: Example

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Matías montes  >  Sorted List  >  Example  >  Download  
File: Example
Role: Example script
Content type: text/plain
Description: An example on how the list can be used
Class: Sorted List
Sort objects of a double linked list
Author: By
Last change:
Date: 2005-07-07 11:30
Size: 1,959 bytes
 

Contents

Class file image Download
<? include_once("class.SortedList.php"); ?>
<html>

<head>
  <title></title>
</head>

<body>

<?php
function comp ($num1$num2)
{
    if (
$num1$num2) return SMALLER;
    if (
$num1$num2) return BIGGER;
    if (
$num1==$num2) return EQUAL;
}

$numbers = array(79,41,41,3,80,90,74,24,83);

$mylist = new SortedList("comp");
echo 
"I'm about to add ".implode(", "$numbers)." to the list.<br>";

foreach (
$numbers as $num)
{
    
$mylist->add($num);
}

$cursor =& $mylist->head->getNext();

echo 
"The List has ".$mylist->size()." elements";

echo 
"<h3>Iterating Forward (increasing order)</h3>";
while (
$cursor->getNext() != NULL){

    echo 
$cursor->getNodeValue();
    echo 
"<br>";
    
$cursor=& $cursor->getNext();
}

echo 
"<h3>Iterating Backwards (decreasing)</h3>";
$cursor =& $mylist->tail->getPrevious();

while (
$cursor->getPrevious() != NULL){

    echo 
$cursor->getNodeValue();
    echo 
"<br>";
    
$cursor=& $cursor->getPrevious();
}

$findme 24;

echo 
"<h3>Now I'll try to find an element</h3>";

$gotit $mylist->fetchElement($findme);
if (
$gotit)
    echo 
"I've found: ".$gotit;
else
    echo 
$findme." is not on the list";

echo 
"<h3>Now i'll erase a caouple of elements and show the resulting list</h3>";

$toerase = array(3418010);

echo 
"Deleting ".implode(", "$toerase)."<br>";

foreach(
$toerase as $e)
{
    
$mylist->deleteElement($e);
}

echo 
"The list contains ".$mylist->size()." elements";

$cursor =& $mylist->head->getNext();
echo 
"<h3>Iterating Forward (increasing order)</h3>";
while (
$cursor->getNext() != NULL){

    echo 
$cursor->getNodeValue();
    echo 
"<br>";
    
$cursor=& $cursor->getNext();
}


echo 
"<h3>Iterating Backwards (decreasing)</h3>";
$cursor =& $mylist->tail->getPrevious();

while (
$cursor->getPrevious() != NULL){

    echo 
$cursor->getNodeValue();
    echo 
"<br>";
    
$cursor=& $cursor->getPrevious();
}


?>
</p>
</body>

</html>