public function testInsertInRandomOrder() { // value => order $values = [1 => 0, 2 => 1, 3 => 1, 4 => 0, 5 => 2]; $expectedValues = [4, 1, 5, 3, 2]; $list = new LinkedList(); foreach ($values as $value => $position) { $list->insert($value, $position); } $this->assertEquals($expectedValues, $list->toArray()); }
return $node; } } return $node; } public function getPrev() { return $this->key() - 1 >= 0 ? $this[$this->key() - 1] : 'NULL'; } public function getNext() { return $this[$this->key() + 1] !== NULL ? $this[$this->key() + 1] : 'NULL'; } } $LL = new LinkedList(); $LL->insert("Akeda"); $LL->insert("Dwi"); $LL->insert("Stevey"); $LL->insert("Paul"); // Function to test given keyword. function test_searching($keyword = '') { global $LL; if ($LL->search($keyword) !== NULL) { echo "Node with value '{$keyword}' is found\n"; echo "The previous node of '{$keyword}' is : " . $LL->getPrev() . "\n"; echo "The next node of '{$keyword}' is : " . $LL->getNext() . "\n"; } else { echo "Node with value '{$keyword}' is NOT found\n"; } echo "\n";