ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Single Linked List
Submitter: Andre Wanderley de Souza (other recipes)
Last Updated: 2006/10/09
Version no: 1.9
Category: Data Structures

 

5 stars 1 vote(s)


Description:

As `throw new Exception' is just avaliable in
php 5, for use php 4 use that call shoud be replaced.

Source: Text Source

<?php
# Author: Andre Souza <ienliven@gmail.com>
# SingleLinkedList Proposal

class SingleLinkedList
{
    var $_vars;
    var $_next;
    
    function SingleLinkedItem()
    {
        $this->_vars = array();
        $this->_next = null;
    }
    
    private function __get($name)
    {
        if($name == "_last")
        {
            $last =& $this;
            while($last->_next != null)
                $last =& $last->_next;
            return $last;
        }
        
        if(isset($this->_vars[$name])) return $this->_vars[$name];
        else 
        {
            throw new Exception("variable: '$name' not found.");
        }
    }
    
    private function __set($name, $value)
    {
        $this->_vars[$name] = $value;
    }
}


# USE:
for($i = 0; $i < 10; $i++)
{
    if(!isset($SLL))
    {
        $SLL = new SingleLinkedList();
        $SLL->name = "Son_{$i}";
    }
    else
    {
        $SLL->_last->_next = new SingleLinkedList($SLL);
        $SLL->_last->name = "Son_{$i}";
    }
}

var_dump($SLL);
?>

Discussion:

As `throw new Exception' is just avaliable in
php 5, for use php 4 use that call shoud be replaced.



Add comment

No comments.



Highest rated recipes:

1. DB_eSession PHP class ...

2. main - python-like if ...

3. iPHP: Semi-interactive ...

4. tinySendMail

5. Microsoft Access ...

6. Single Linked List

7. PHP MySQL Search Class

8. Pass Javascript arrays ...

9. Microsoft Access ...

10. Password generation




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.