<?php

class LinkedList
{
    /**
     * The data stored in the list.
     *
     * @var array
     * @access private
     */
    var $_nodes = array();

    /**
     * A reference to the current node being worked on.
     *
     * @var object
     * @access private
     */
    var $_current = null;

    function LinkedList()
    {

    }

    function add($data)
    {
        $this->_nodes[] = $data;
    }

    function insert($data, $position)
    {
        if ($position >= count($this->_nodes)) {
            $position = count($this->_nodes);
        } else if ($position < 0) {
            $position = 0;
        }

        for($i = count($this->_nodes)-1; $i >= $position; $i--) {
            $this->_nodes[$i+1] = $this->_nodes[$i];
        }

        $this->_nodes[$position] = $data;
    }

    function remove($position)
    {
        if (isset($this->_nodes[$position])) {
            unset($this->_nodes[$position]);
        }

        for($i = $position+1; $i <= count($this->_nodes); $i++) {
            $this->_nodes[$i-1] = $this->_nodes[$i];
        }
        unset($this->_nodes[count($this->_nodes)-1]);
    }

    function moveUp($position)
    {
        if (isset($this->_nodes[$position])) {
            $temp = $this->_nodes[$position];
            $this->remove($position);
            $this->insert($temp, $position-1);
        }
    }

    function moveDown($position)
    {
        if (isset($this->_nodes[$position])) {
            $temp = $this->_nodes[$position];
            $this->insert($temp, $position+1);
            $this->remove($position);
        }
    }

    function nextNode()
    {
        next($this->_nodes);
    }

    function previousNode()
    {
        prev($this->_nodes);
    }

    function getCurrent()
    {
        return current($this->_nodes);
    }

    function getNode($position)
    {
        if (isset($this->_nodes[$position])) {
            return $this->_nodes[$position];
        }
    }
}
?>