Login   Register  
PHP Classes
elePHPant
Icontem

File: LinkedListExample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Oddleif Halvorsen  >  Double Linked List  >  LinkedListExample.php  
File: LinkedListExample.php
Role: Example script
Content type: text/plain
Description: Examples on how to use the LinkedList and it iterators.
Class: Double Linked List
A double linked list similar to LinkedList in Java
 

Contents

Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
include_once("LinkedList.php");

class 
test{
    var 
$testVar;
    
    function 
test($testVal){
        
$this->testVar $testVal;
    }
    
    function 
getValue(){
        return 
$this->testVar;
    }
}

$list = new LinkedList();
?>
<h1>Test script for the LinkedList</h1>
<p>This test consists of the following elements:</p>
<ol>
  <li>Create a list with 10 elements, and prints it different ways to test the references</li>
  <li>Remove some elements from the list, and print the same way again to test the references</li>
  <li>Insert the removed elements again and once more print the list to test the refernces. </li>
</ol>
<h2>Part one: Inserts 10 objects to the list</h2>
<?php
for($i=0$i<10$i++){
    
$list->add(new test(($i+1)));
    echo 
"<p>Index: " $i " - value: " . ($i+1) . "</p>";
}
?>
<p>----------------------------------------------------</p>
<h3>Gets all the values in the list using the get(index) function</h3>
<?php
for($i=0$i<$list->size(); $i++){
    
$object $list->get($i);
    echo 
"<p>Index: " $i " - value: " $object->getValue() . "</p>";
}
?>
<p>----------------------------------------------------</p>
<h3>Prints the list with a Iterator</h3>
<?php
$i
=0;
$iterator $list->iterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
?>
<p>----------------------------------------------------</p>
<h3>Prints the list with a ListIterator</h3>
<?php
$i
=0;
$iterator $list->listIterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
?>
<p>----------------------------------------------------</p>
<h3>Walks up and down the list using the ListIterator:</h3>
<?php
$i
=0;
$iterator $list->listIterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
$i -= 2;
while(
$iterator->hasPrevious()){
    
$tmpObject $iterator->getPrevious();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i--;
}
?>

<p>----------------------------------------------------</p>
<h2>Part 2: Removes some objects</h2>
<?php
$i
=0;
$iterator $list->iterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    if(
$tmpObject->getValue() == || $tmpObject->getValue() == || $tmpObject->getValue() == 10){
        echo 
"<p>Removing: Index: " $i " - value: " $tmpObject->getValue() . "</p>";
        
$iterator->remove();
    }
    else
        echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
echo 
"<p>List size: " $list->size() . "</p>";
?>
<p>----------------------------------------------------</p>
<h3>Gets all the values in the list using the get(index) function</h3>
<?php
for($i=0$i<$list->size(); $i++){
    
$object $list->get($i);
    echo 
"<p>Index: " $i " - value: " $object->getValue() . "</p>";
}
?>
<p>----------------------------------------------------</p>
<h3>Prints the list with a Iterator</h3>
<?php
$i
=0;
$iterator $list->iterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
?>
<p>----------------------------------------------------</p>
<h3>Prints the list with a ListIterator</h3>
<?php
$i
=0;
$iterator $list->listIterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
?>
<p>----------------------------------------------------</p>
<h3>Walks up and down the list using the ListIterator:</h3>
<?php
$i
=0;
$iterator $list->listIterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
$i -= 2;
while(
$iterator->hasPrevious()){
    
$tmpObject $iterator->getPrevious();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i--;
}
?>
<p>----------------------------------------------------</p>
<h2>Part 3: Inserts the removed objects again</h2>
<?php
$list
->add(new test(10));
echo 
"<p>appends 10 to the list</p>";
$list->addAtPosition(4,new test(6));
echo 
"<p>inserts 6 to the list at index (the CURRENT(!!!) index) 4</p>";
$list->addAtPosition(0, new test(1));
echo 
"<p>inserts 1 to the beginning of the list</p>";
?>
<p>----------------------------------------------------</p>
<h3>Gets all the values in the list using the get(index) function</h3>
<?php
for($i=0$i<$list->size(); $i++){
    
$object $list->get($i);
    echo 
"<p>Index: " $i " - value: " $object->getValue() . "</p>";
}
?>
<p>----------------------------------------------------</p>
<h3>Prints the list with a Iterator</h3>
<?php
$i
=0;
$iterator $list->iterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
?>
<p>----------------------------------------------------</p>
<h3>Prints the list with a ListIterator</h3>
<?php
$i
=0;
$iterator $list->listIterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
?>
<p>----------------------------------------------------</p>
<h3>Walks up and down the list using the ListIterator:</h3>
<?php
$i
=0;
$iterator $list->listIterator();
while(
$iterator->hasNext()){
    
$tmpObject $iterator->getNext();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i++;
}
$i -= 2;
while(
$iterator->hasPrevious()){
    
$tmpObject $iterator->getPrevious();
    echo 
"<p>Index: " $i " - value: " $tmpObject->getValue() . "</p>";
    
$i--;
}
?>
<p>----------------------------------------------------</p>
<p>End of test</p>

</body>
</html>