Example #1
0
 public function testLen()
 {
     $l = new DoublyLinkedList();
     $this->assertEquals(0, $l->len());
     // pushFront and pushBack will increases list's length.
     $e1 = $l->pushFront(1);
     $e2 = $l->pushFront(2);
     $e3 = $l->pushBack(3);
     $e4 = $l->pushBack('four');
     $this->assertEquals(4, $l->len());
     // remove will decreases list's length.
     $l->remove($e1);
     $this->assertEquals(3, $l->len());
     // remove inexistsent element won't decrease list's length.
     $l->remove($e1);
     $this->assertEquals(3, $l->len());
     $l->remove($e2);
     $l->remove($e3);
     $l->remove($e4);
     $this->assertEquals(0, $l->len());
 }
Example #2
0
        if ($node == NULL) {
            //if $node points to NULL
            echo "Linked List is empty";
            return NULL;
        }
        while ($node->next != NULL) {
            echo "Node value is " . $node->value . "<br />";
            $node = $node->next;
            //now have $node point to where $node->next is pointing to
        }
        echo "Node value is " . $node->value . "<br />";
        //have it print the last node value
    }
}
$dll = new DoublyLinkedList();
//creates a new instance of the doubly linked list
$dll->add(5);
//should add a new node with the value of 5
$dll->add(7);
//should add a new node with the value of 7
$dll->add(10);
$dll->removeAllNodesWithValueOf(7);
//should remove all nodes with value of 7
$dll->remove(1);
//removes node of position 1
$dll->add(3);
//should add a new node with the value of 3
$dll->insert(1, 100);
//insert a new node of value 100 in the position 1
$dll->printValues();
//should print all the values in the doubly linked list