public function insertNode($new_node, $existing_node, $before = false)
    {
        $pos = 0;
        $exist_pos = $existing_node->getPosition();
        $this->rewind();

        // Now add the node according to the requested mode
        switch ($before) {

        case true:
            $this->add($exist_pos, $new_node);
            break;

        // after
        case false:
            if ($this->offsetExists($exist_pos + 1)) {
                $this->add($exist_pos + 1, $new_node);
            } else {
                $this->appendNode($new_node);
                return true;
            }
            break;
        }

        // Fix positions
        $this->rewind();
        while ($n = $this->current()) {
            $n->setPosition($pos);
            $this->next();
            $pos++;
        }

        return true;
    }
public function insertNode($new_node, $existing_node, $before = false)
    {
        $pos = 0;
        $exist_pos = $existing_node->getPosition();
        $this->rewind();

        // Now add the node according to the requested mode
        switch ($before) {

        case true:
            $this->add($exist_pos, $new_node);
            break;

        // after
        case false:
            if ($this->offsetExists($exist_pos + 1)) {
                $this->add($exist_pos + 1, $new_node);
            } else {
                $this->appendNode($new_node);
                return true;
            }
            break;
        }

        // Fix positions
        $this->rewind();
        while ($n = $this->current()) {
            $n->setPosition($pos);
            $this->next();
            $pos++;
        }

        return true;
    }
public function insertNode ( $ new_node , $ existing_node , $ before = false ) { $ pos = 0 ; $ exist_pos = $ existing_node -> getPosition ( ) ; $ this -> rewind ( ) ; // Now add the node according to the requested mode switch ( $ before ) { case true : $ this -> add ( $ exist_pos , $ new_node ) ; break ; // after case false : if ( $ this -> offsetExists ( $ exist_pos + 1 ) ) { $ this -> add ( $ exist_pos + 1 , $ new_node ) ; } else { $ this -> appendNode ( $ new_node ) ; return true ; } break ; } // Fix positions $ this -> rewind ( ) ; while ( $ n = $ this -> current ( ) ) { $ n -> setPosition ( $ pos ) ; $ this -> next ( ) ; $ pos ++ ; } return true ; }
Inserts a node into the linked list, based on a reference node that
already exists in the list.

@param mixed $new_node      New node to add to the list
@param mixed $existing_node Reference position node
@param bool  $before        Insert new node before or after the existing node

@return bool Success or failure
Inserts a node into the linked list based on a reference node that already exists in the list .