public function testGetIntersectingNode() { $a1 = new Node("one"); $a2 = new Node("two"); $a3 = new Node("three"); $a4 = new Node("four"); $a5 = new Node("five"); $a1->setNext($a2); $a2->setNext($a3); $a3->setNext($a4); $a4->setNext($a5); $b1 = new Node("un"); $b2 = new Node("deux"); $b3 = new Node("trois"); $b1->setNext($b2); $b2->setNext($b3); $this->assertNull(LinkedListIntersectionChecker::getIntersectingNode($a1, $b1)); $c1 = new Node("uno"); $c2 = new Node("dos"); $c3 = new Node("tres"); $c1->setNext($c2); $c2->setNext($c3); $a5->setNext($c1); $b3->setNext($c1); $this->assertSame($c1, LinkedListIntersectionChecker::getIntersectingNode($a1, $b1)); }
public function testGetIntersectingNode() { $a1 = new Node("one"); $a2 = new Node("two"); $a3 = new Node("three"); $a4 = new Node("four"); $a5 = new Node("five"); $a1->setNext($a2); $a2->setNext($a3); $a3->setNext($a4); $a4->setNext($a5); $b1 = new Node("un"); $b2 = new Node("deux"); $b3 = new Node("trois"); $b1->setNext($b2); $b2->setNext($b3); $this->assertNull(LinkedListIntersectionCheckerNoHashMap::getIntersectingNode($a1, $b1)); $c1 = new Node("uno"); $c2 = new Node("dos"); $c3 = new Node("tres"); $c1->setNext($c2); $c2->setNext($c3); $a5->setNext($c1); $b3->setNext($c1); $this->assertSame($c1, LinkedListIntersectionCheckerNoHashMap::getIntersectingNode($a1, $b1)); // disconnect the first 4 nodes of the linked list. // now it begins @ $a5 $a4->setNext(null); $this->assertSame($c1, LinkedListIntersectionCheckerNoHashMap::getIntersectingNode($a5, $b1)); }
public function push($value) { $newHead = new Node($value); $newHead->setNext($this->linkedList); $this->linkedList = $newHead; $this->size++; }
/** * All object initialized with NULL as its head. * * @return void */ public function __construct() { // Sentinel is dummy object before head and after tail. $sentinel = new Node(NULL); $sentinel->setNext($sentinel); $sentinel->setPrev($sentinel); $this->_sentinel = $sentinel; $this->_head = $this->_sentinel; }
public static function deleteNodeFromLinkedList(Node $node) { if ($node === null) { throw new InvalidArgumentException('node is null'); } $next = $node->getNext(); if ($next === null) { throw new InvalidArgumentException('node is not in the middle of a linked list'); } // overwrite values in the deleted node with the ones from the next node. $node->setData($next->getData()); $node->setNext($next->getNext()); }
public static function toLinkedListOfDigits($number) { $head = null; $base = 1; while ($number > 0) { $nextBase = $base * 10; $remainder = $number % $nextBase; $digit = new Node($remainder / $base); $digit->setNext($head); $head = $digit; $number -= $remainder; $base = $nextBase; } return $head; }
public function testGetIntersectingNode() { $n1 = new Node("one"); $n2 = new Node("two"); $n3 = new Node("three"); $n4 = new Node("four"); $n5 = new Node("five"); $n1->setNext($n2); $n2->setNext($n3); $n3->setNext($n4); $n4->setNext($n5); $this->assertNull(LinkedListCycleDetector::getCycleNode($n1)); // create a cycle $n5->setNext($n3); $this->assertSame($n3, LinkedListCycleDetector::getCycleNode($n1)); }
public function push($value) { $newHead = new Node($value); if ($this->linkedList === null) { $this->linkedList = $newHead; $this->min = new Node($value); } else { $newHead->setNext($this->linkedList); $this->linkedList = $newHead; if ($value < $this->min->getData()) { $newMin = new Node($value); $newMin->setNext($this->min); $this->min = $newMin; } } }
public static function partition(Node $node, $x) { $head = $node; $previousNode = null; while ($node !== null) { if ($node->getData() < $x && $previousNode !== null) { // remove the node from this part of the list $previousNode->setNext($node->getNext()); // put this node at the begining of the list $node->setNext($head); // reset head $head = $node; // reset node for the next iteration $node = $previousNode; } else { $previousNode = $node; $node = $node->getNext(); } } return $head; }
public function ListInsert($i, $data) { //后插 $j = 1; //从第一个元素开始遍历 $node = $this->_headNode; // 指向头结点 while ($node && $j < $i) { $node = $node->getNext(); $j++; } if (!$node || $j > $i) { // $i个结点不存在时 return "err"; } $newNode = new Node(); $newNode->setData($data); $newNode->setNext($node->getNext()); $node->setNext($newNode); return true; }
public function testSumWithMultipleCarryOperations() { $a1 = new Node(1); $b1 = new Node(9); $b2 = new Node(9); $b3 = new Node(9); $b4 = new Node(9); $b5 = new Node(9); $b1->setNext($b2); $b2->setNext($b3); $b3->setNext($b4); $b4->setNext($b5); $node = SumListNoConvert::sum($a1, $b1); $this->assertEquals(0, $node->getData()); $node = $node->getNext(); $this->assertEquals(0, $node->getData()); $node = $node->getNext(); $this->assertEquals(0, $node->getData()); $node = $node->getNext(); $this->assertEquals(0, $node->getData()); $node = $node->getNext(); $this->assertEquals(0, $node->getData()); $node = $node->getNext(); $this->assertEquals(1, $node->getData()); $node = $node->getNext(); $this->assertNull($node); }
/** * Adds a node to the head of the list * @param Node $head the node object that represents the head of the list * @param Node $node the node to move to the head of the list */ private function attach($head, $node) { $node->setPrevious($head); $node->setNext($head->getNext()); $node->getNext()->setPrevious($node); $node->getPrevious()->setNext($node); }
public function testToInteger() { $n1 = new Node(6); $n2 = new Node(1); $n3 = new Node(7); $n1->setNext($n2); $n2->setNext($n3); $this->assertEquals(617, SumListReversed::toInteger($n1)); }
public function testFindBeginningOfCycleWithCircularListOfSizeFive() { $n1 = new Node("one"); $n2 = new Node("two"); $n3 = new Node("three"); $n4 = new Node("four"); $n5 = new Node("five"); $n1->setNext($n2); $n2->setNext($n3); $n3->setNext($n4); $n4->setNext($n5); $n5->setNext($n1); $cycleNode = LinkedListCycleDetector::findBeginningOfCycle($n1); $this->assertSame($n1, $cycleNode, 'Expected: ' . $n1->getData() . ' Found: ' . $cycleNode->getData()); }