public function testPushBackList() { $l = new DoublyLinkedList(); $e1 = $l->pushFront(1); $e2 = $l->pushFront(2); $e3 = $l->pushBack(3); $other = new DoublyLinkedList(); $e4 = $other->pushBack(4); $e5 = $other->pushBack(5); $l->pushBackList($other); $this->assertEquals(5, $l->len()); $this->assertNotEquals($e5, $l->getBack()); $this->assertEquals(5, $l->getBack()->getValue()); $this->assertEquals(4, $l->getBack()->getPrev()->getValue()); $this->assertEquals($e3, $l->getBack()->getPrev()->getPrev()); // pushBackList the same list will double its elements. $other->pushBackList($other); $this->assertEquals(4, $other->len()); $this->assertEquals(5, $other->getBack()->getValue()); $this->assertEquals(4, $other->getBack()->getPrev()->getValue()); $this->assertEquals(5, $other->getBack()->getPrev()->getPrev()->getValue()); // pushBackList empty list won't alter the list. $emptyList = new DoublyLinkedList(); $other->pushBackList($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); } }