A linked list library that keeps values sorted.
use PolakJan\SortedLinkedList\SortedLinkedList;
$list = new SortedLinkedList;
$list->insert(0);
$list->insert(1);
$list->insert(3);
$list->insert(1);
$list->insert(2);
// the list now contains values (in order) 0, 1, 1, 2, 3
The order can be set to asc
or desc
anytime.
$list->setOrder('desc');
// the list now contains values (in order) 3, 2, 1, 1, 0
The locale (as per FRC 4646) defines how string values would be compared.
For string comparison to work the intl
extension must be installed.
$list->setLocale('fr_FR');
$list->length();
$value = $list->seek(1);
// 2
$list->remove(2);
// the list now contains values (in order) 3, 2, 1, 0
$list->shift();
// the list now contains values (in order) 2, 1, 0
$list->pop();
// the list now contains values (in order) 2, 1
$array = $list->toArray();
// [2, 1]
foreach ($list as $i => $value) {
echo $i . ':' . $value . ", ";
}
// 0:2, 1:1