public function testIsNotPalindrome()
 {
     $list = new LinkedList();
     $list->add("A");
     $list->add("B");
     $list->add("C");
     $list->add("D");
     $this->assertFalse(LinkedListPalindromeChecker::isPalindrome($list));
 }
 public function testLinkedList()
 {
     $list = new LinkedList();
     $this->assertEquals(0, $list->getSize());
     $this->assertTrue($list->isEmpty());
     $this->assertNull($list->peekFirst());
     $this->assertNull($list->peekLast());
     $this->assertNull($list->removeFirst());
     $this->assertNull($list->removeLast());
     $count = 0;
     foreach ($list as $key => $value) {
         $count++;
     }
     $this->assertEquals(0, $count);
     $list->add("one");
     $this->assertEquals(1, $list->getSize());
     $this->assertFalse($list->isEmpty());
     $list->add("two");
     $this->assertEquals(2, $list->getSize());
     $this->assertFalse($list->isEmpty());
     $list->add("three");
     $this->assertEquals(3, $list->getSize());
     $this->assertFalse($list->isEmpty());
     $expected = ["one", "two", "three"];
     $i = 0;
     foreach ($list as $key => $value) {
         $this->assertEquals($i, $key);
         $this->assertEquals($expected[$i], $value);
         $i++;
     }
     $this->assertEquals(3, $i);
     $this->assertEquals("one", $list->peekFirst());
     $this->assertEquals("one", $list->removeFirst());
     $this->assertEquals(2, $list->getSize());
     $this->assertEquals("three", $list->peekLast());
     $this->assertEquals("three", $list->removeLast());
     $this->assertEquals(1, $list->getSize());
     $this->assertEquals("two", $list->peekFirst());
     $this->assertEquals("two", $list->removeFirst());
     $this->assertEquals(0, $list->getSize());
     $this->assertTrue($list->isEmpty());
     $list->add("four");
     $this->assertEquals(1, $list->getSize());
     $this->assertFalse($list->isEmpty());
     foreach ($list as $key => $value) {
         $this->assertEquals(0, $key);
         $this->assertEquals("four", $value);
     }
     $this->assertEquals("four", $list->peekFirst());
     $this->assertEquals("four", $list->removeLast());
     $this->assertEquals(0, $list->getSize());
     $this->assertTrue($list->isEmpty());
 }
 public function testGetDepths()
 {
     $firstLevel1 = new BinaryTreeNode("first level 1");
     $secondLevel1 = new BinaryTreeNode("second level 1");
     $secondLevel2 = new BinaryTreeNode("second level 2");
     $thirdLevel1 = new BinaryTreeNode("third level 1");
     $thirdLevel2 = new BinaryTreeNode("third level 2");
     $thirdLevel3 = new BinaryTreeNode("third level 3");
     $fourthLevel1 = new BinaryTreeNode("fourth level 1");
     $firstLevel1->setLeft($secondLevel1);
     $firstLevel1->setRight($secondLevel2);
     $secondLevel1->setRight($thirdLevel1);
     $secondLevel2->setLeft($thirdLevel2);
     $secondLevel2->setRight($thirdLevel3);
     $thirdLevel2->setLeft($fourthLevel1);
     $firstLevel = new LinkedList();
     $firstLevel->add($firstLevel1);
     $secondLevel = new LinkedList();
     $secondLevel->add($secondLevel1);
     $secondLevel->add($secondLevel2);
     $thirdLevel = new LinkedList();
     $thirdLevel->add($thirdLevel1);
     $thirdLevel->add($thirdLevel2);
     $thirdLevel->add($thirdLevel3);
     $fourthLevel = new LinkedList();
     $fourthLevel->add($fourthLevel1);
     $expected = [$firstLevel, $secondLevel, $thirdLevel, $fourthLevel];
     $this->assertEquals($expected, DFSDepthLister::getDepths($firstLevel1));
     $this->assertEquals($expected, BFSDepthLister::getDepths($firstLevel1));
 }
 public static function getDepths(BinaryTreeNode $n)
 {
     $depths = [];
     $nodes = new LinkedList();
     $nodes->add($n);
     while ($nodes !== null) {
         $childNodes = new LinkedList();
         foreach ($nodes as $node) {
             $left = $node->getLeft();
             if ($left !== null) {
                 $childNodes->add($left);
             }
             $right = $node->getRight();
             if ($right !== null) {
                 $childNodes->add($right);
             }
         }
         $depths[] = $nodes;
         $nodes = !$childNodes->isEmpty() ? $childNodes : null;
     }
     return $depths;
 }
 private static function populateDepths(array &$depths, BinaryTreeNode $n, $depth)
 {
     $nodeList = $depth < count($depths) ? $depths[$depth] : null;
     if ($nodeList === null) {
         $nodeList = new LinkedList();
         $depths[$depth] = $nodeList;
     }
     $nodeList->add($n);
     $left = $n->getLeft();
     if ($left !== null) {
         self::populateDepths($depths, $left, $depth + 1);
     }
     $right = $n->getRight();
     if ($right !== null) {
         self::populateDepths($depths, $right, $depth + 1);
     }
 }
Beispiel #6
0
        $this->tail->next = $node;
        $this->tail = $this->tail->next;
        $this->size++;
    }
    public function get($pos)
    {
        return $this->go($pos)->next->data;
    }
    private function go($pos)
    {
        $node = $this->head;
        for ($i = 0; $i < $pos; $i++) {
            $node = $node->next;
        }
        return $node;
    }
    public function size()
    {
        return $this->size;
    }
}
$linkedlis = new LinkedList();
$linkedlis->add('2');
$linkedlis->add('4');
$linkedlis->add('6');
$linkedlis->add('8');
echo $linkedlis->size();
echo $linkedlis->get(0);
echo $linkedlis->get(1);
echo $linkedlis->get(2);
echo $linkedlis->get(3);
Beispiel #7
0
 /**
  * Add an entry to the cache. If there's already an
  * entry then for this host then the entry will be
  * replaced.
  */
 public function put(\blaze\lang\String $host, \blaze\lang\Object $address)
 {
     $policy = $this->getPolicy();
     if ($policy == InetAddressCachePolicy::NEVER) {
         return $this;
     }
     // purge any expired entries
     if ($policy != InetAddressCachePolicy::FOREVER) {
         // As we iterate in insertion order we can
         // terminate when a non-expired entry is found.
         $expired = new LinkedList();
         $i = $cache->keySet()->iterator();
         $now = System::currentTimeMillis();
         while ($i->hasNext()) {
             $key = $i->next();
             $entry = $cache->get($key);
             if ($entry->expiration >= 0 && $entry->expiration < $now) {
                 $expired->add($key);
             } else {
                 break;
             }
         }
         $i = $expired->iterator();
         while ($i->hasNext()) {
             $cache->remove($i->next());
         }
     }
     // create new entry and add it to the cache
     // -- as a HashMap replaces existing entries we
     //    don't need to explicitly check if there is
     //    already an entry for this host.
     $expiration;
     if ($policy == InetAddressCachePolicy::FOREVER) {
         $expiration = -1;
     } else {
         $expiration = System::currentTimeMillis() + $policy * 1000;
     }
     $entry = new CacheEntry($address, $expiration);
     $cache->put($host, $entry);
     return $this;
 }
 public function add($e)
 {
     assert($this->size() < $this->getExpectedSize());
     return parent::add($e);
 }