public function testLinkList()
 {
     $totalNodes = 100;
     $theList = new LinkList();
     for ($i = 1; $i <= $totalNodes; $i++) {
         $theList->insertLast($i);
     }
     $this->assertEquals($totalNodes, $theList->totalNodes());
     for ($i = 1; $i <= $totalNodes; $i++) {
         $theList->insertFirst($i);
     }
     $totalNodes = $totalNodes * 2;
     $this->assertEquals($totalNodes, $theList->totalNodes());
     $theList->reverseList();
     $this->assertEquals($totalNodes, $theList->totalNodes());
     $theList->deleteFirstNode();
     $this->assertEquals($totalNodes - 1, $theList->totalNodes());
     $theList->deleteLastNode();
     $this->assertEquals($totalNodes - 2, $theList->totalNodes());
     /* Delete node which has a value of '5' */
     $theList->deleteNode(5);
     $this->assertEquals($totalNodes - 3, $theList->totalNodes());
     /* Insert a node at the end of the list with a value of '22' */
     $theList->insertLast(22);
     $this->assertEquals($totalNodes - 2, $theList->totalNodes());
     /* Find a node with a value of '25' (is in the list) */
     $found = $theList->find(25);
     $this->assertEquals(25, $found->data);
     /* Find a node with a value of '125' (is not in the list) */
     $found = $theList->find(125);
     $this->assertNull($found);
     /* Return the data stored in the node at position '50'
        which is in the list */
     $data = $theList->readNode(50);
     $this->assertEquals(50, $data);
     /* Return the data stored in the node at position '450'
        which is not in the list */
     $data = $theList->readNode(450);
     $this->assertNull($data);
 }
Ejemplo n.º 2
0
    }
    //empty linklist
    public function emptyList()
    {
        $this->firstNode == NULL;
    }
    //insertion at index
    public function insert($NewItem, $key)
    {
        if ($key == 0) {
            $this->insertFirst($NewItem);
        } else {
            $link = new ListNode($NewItem);
            $current = $this->firstNode;
            $previous = $this->firstNode;
            for ($i = 0; $i < $key; $i++) {
                $previous = $current;
                $current = $current->next;
            }
            $previous->next = $link;
            $link->next = $current;
            $this->count++;
        }
    }
}
$obj = new LinkList();
$obj->insertFirst($value);
$obj->insert($value, $key);
// at any index
$obj->deleteNode($value);
$obj->readList();