0

well ive been doing some work with object oriented php and came across a question i couldnt answer. can you create a list i.e. similiar to a linkedlist in java or the like in php ?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Fungus1487
0

Yes not a problem, this the code for a list node. Writting the rest of the operation functions are quite easy once you have a node.

<?php 
class ListNode{ 
    var $previousNode; 
    var $nextNode; //a reference to the next node 
    var $element; //the object || value for this node. 
     
    /** 
     * Constructs a ListNode. 
     * @param $element The object/value of the node 
     */ 
    function ListNode($element = NULL){ 
        $this->element = &$element; 
    } 
     
    /** 
     * Sets the reference to the previous node. 
     * @param &$node The previous node. 
     */ 
    function setPrevious($node){ 
        $this->previousNode = &$node; 
    } 
     
    /** 
     * Sets the reference to the next node. 
     * @param &$node The next node. 
     */ 
    function setNext($node){ 
        $this->nextNode = &$node; 
    } 
     
    /** 
     * Returns the reference to the previous node. 
     * @return The reference to the previous node. 
     */ 
    function &getPrevious(){ 
        return $this->previousNode; 
    } 

    /** 
     * Returns the reference to the next node. 
     * @return The reference to the next node. 
     */ 
    function &getNext(){ 
        return $this->nextNode; 
    } 

    /** 
     * Returns the object/value of the node 
     * @return The object/value of the node 
     */ 
    function getNodeValue(){ 
        return $this->element; 
    } 
} 
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.