Ejemplo n.º 1
0
 public static function testList()
 {
     $linkedList = new LinkedList();
     if ($linkedList->getCount() !== 0) {
         self::printError("problem creating a new list - count not 0");
     }
     // test insertFront
     $linkedList->insertFront('insertFront');
     if ($linkedList->getCount() !== 1) {
         self::printError("problem adding first element to front - count not 1");
     }
     // ad a second item to the front
     $linkedList->insertFront('insertFrontAgain');
     if ($linkedList->getCount() !== 2) {
         self::printError("problem adding second element to front - count not 2");
     }
     // test getAt
     if ($linkedList->getAt(0) !== 'insertFrontAgain') {
         self::printError("problem with getAt: not finding first node");
     }
     // test getAt
     if ($linkedList->getAt(12) !== null) {
         self::printError("problem with getAt: accessing nonexisting node should have returned null");
     }
     // test insertEnd
     $linkedList->insertEnd('insertEnd');
     if ($linkedList->getAt(2) !== 'insertEnd' || $linkedList->getCount() !== 3) {
         self::printError("problem with insertEnd: inserted item not found");
     }
     // test insertAt
     $linkedList->insertAt('insertedAt2', 2);
     if ($linkedList->getAt(2) !== 'insertedAt2' || $linkedList->getAt(3) !== 'insertEnd') {
         self::printError("problem with insertAt: inserted value not found at proper key");
     }
     $linkedList->insertAt('insertedAt3', 3);
     if ($linkedList->getAt(3) !== 'insertedAt3') {
         self::printError("problem with insertAt: inserted value not found at proper key");
     }
     // test deleteAt
     $linkedList->deleteAt(3);
     if ($linkedList->getAt(3) === 'insertedAt3') {
         self::printError("problem with deleteAt: deleted node still present");
     }
     // test deleting by value
     $linkedList->delete('insertedAt2');
     // test reverse
     $linkedList->reverse('recursive');
     if ($linkedList->getAt(0) !== 'insertEnd') {
         self::printError("problem with reverse");
     }
     $linkedList->reverse();
     // display... for now
     // $linkedList->debug();
     $linkedList->printList('reverse');
     // test empty
     $linkedList->emptyList();
     if ($linkedList->getCount() !== 0) {
         self::printError("problem with emptyList: list count not zero");
     }
 }
    }
}
$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";
}
// Test searching of node in linked list
test_searching("Akeda");
test_searching("Someone else");
test_searching("Dwi");
test_searching("Paul");
// Delete Stevey
$LL->delete("Stevey");
// The next and prev references were updated after delete operation.
echo "After 'Stevey' is deleted: \n\n";
test_searching("Dwi");
test_searching("Paul");
Ejemplo n.º 3
0
 /**
  * @expectedException Exception
  * @expectedExceptionMessageRegExp #Can't delete#
  */
 public function testErrorDeletePositionNotExists()
 {
     $list = new LinkedList([1, 2, 3]);
     $list->delete(100);
 }