public function testPushFrontList() { $l = new DoublyLinkedList(); $e1 = $l->pushFront(1); $e2 = $l->pushBack(2); $e3 = $l->pushBack(3); $other = new DoublyLinkedList(); $e4 = $other->pushBack(4); $e5 = $other->pushBack(5); $l->pushFrontList($other); $this->assertEquals(5, $l->len()); $this->assertEquals(4, $l->getFront()->getValue()); $this->assertEquals(5, $l->getFront()->getNext()->getValue()); $this->assertEquals($e1, $l->getFront()->getNext()->getnext()); // pushFrontList the same list will double its elements. $other->pushFrontList($other); $this->assertEquals(4, $other->len()); $this->assertEquals(4, $other->getFront()->getValue()); $this->assertEquals(5, $other->getFront()->getNext()->getValue()); $this->assertEquals(4, $other->getFront()->getNext()->getNext()->getValue()); // pushFrontList empty list won't alter the list. $emptyList = new DoublyLinkedList(); $other->pushFrontList($emptyList); $this->assertEquals(4, $other->len()); }
/** * Inserts a copy of an other list at the front of list. * * Other list and current list may be the same. * * @param DoublyLinkedList $other Other list to insert * * @api */ public function pushFrontList(DoublyLinkedList $other) { for ($i = $other->len(), $el = $other->getBack(); $i > 0; $i--, $el = $el->getPrev()) { $this->insertValue($el->getValue(), $this->root); } }