예제 #1
0
    }
    public function add($val)
    {
        //return true if added correctly
        //check to see if there is not a head because than we can create the head node
        if ($this->head == null) {
            //point head to that new node
            $this->head = new Node($val);
        } else {
            //we have a head lets navigate through the Linked List till we get to the end
            //when we reach the end we can add the new node there
            //temp to help us navigate the linked list
            $current = $this->head;
            //while loops are extremely skilled at looping through a linked list
            while ($current->next) {
                //move our pointer forward to the next node
                $current = $current->next;
            }
            //we reached the end! time to add the new node to the end
            $current->next = new Node($val);
        }
    }
}
//now we can execute our new function to add nodes to a linked list!
$newList2 = new SinglyLinkedList();
$newList2->add(1);
$newList2->add(2);
$newList2->add(3);
$newList2->add(4);
$newList2->add(5);
var_dump($newList2);
예제 #2
0
            return null;
        }
        while ($current->next != null) {
            echo $current->val . "<br>";
            $current = $current->next;
        }
        echo $current->val;
    }
    public function find($value)
    {
        if ($current == null) {
            echo "empty";
            return null;
        }
        //return node if value is found
        //return false if not found
    }
    public function isEmpty()
    {
        //return true if the linked list is empty
        //return false if linked list has nodes
    }
}
$list = new SinglyLinkedList();
$list->add('Steven');
$list->add('Bobby');
$list->add('Mr Poopybutthole');
var_dump($list);
?>

예제 #3
0
            if ($current->next == null) {
                return false;
            } else {
                $current = $current->next;
            }
        }
        return $current->value;
        //return node if value is found
        //return false if not found
    }
    public function isEmpty()
    {
        if ($this->count = 0) {
            return true;
        } else {
            return false;
        }
        //return true if the linked list is empty
        //return false if linked list has nodes
    }
}
$list = new SinglyLinkedList();
$list->add("John");
$list->add("Todd");
$list->add("Tim");
$list->add("Pat");
$list->add("Joey");
$list->add("Lewis");
$list->add("Michael");
$list->insert("Todd", "Elena");
$list->printList();