1. Go to this page and download the library: Download obernard/linkedlist library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// MyList.php
class MyList extends AbstractSinglyLinkedList {
}
// MyNode.php
class MyNode extends AbstractSinglyLinkedNode {
public $data;
public function __construct($data) {
$this->data = $data;
}
// IterableNodeInterface getValue method:
public function getValue() {
return $this->data;
}
}
// program.php
$list= new MyList();
$list->pushToHead(new MyNode("test1"))->pushToHead(new MyNode("test2"));
foreach ($list as $key => $value) {
// do something with $value string (returned by MyNode->getValue()) and $key (MyNode->getKey())
}
// AbstractNode.php
/**
* Returns the node's rank beginning at tail node (ie at the end).
* !! Time complexity is O(n) !!
* @return int
*/
public function distanceToLastNode():int {
if ($this->isLast()) // if you Node are the last node just say 0
return 0;
else {
// just ask your next node for its rank and increment
$nextNodeRrank=$this->next->distanceToLastNode();
return ++$nextNodeRrank;
}
}
// very low 0(n) < perf < 0(n^2)
for ($i=0; $i<$list->length(); $i++) {
$list->head($i);
}
// very very low perf ~ O(n^2) ...
for ($i=0; $i<$list->length(); $i++) {
$list->head($i)->distanceToLastNode();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.