PHP code example of d-skora / simple-sorted-linked-list
1. Go to this page and download the library: Download d-skora/simple-sorted-linked-list 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/ */
d-skora / simple-sorted-linked-list example snippets
use SimpleSortedLinkedList\LinkedList\SortedLinkedList;
use SimpleSortedLinkedList\LinkedList\SortOrder;
$list = SortedLinkedList::create([3, 1, 4, 1, 5, 9, 2], SortOrder::ascending());
$list->toArray(); // [1, 1, 2, 3, 4, 5, 9]
$list->first(); // 1
$list->last(); // 9
$list->at(2); // 2
$list->count(); // 7
$list = SortedLinkedList::create(['banana', 'apple', 'cherry'], SortOrder::descending());
$list->toArray(); // ['cherry', 'banana', 'apple']
// Sort integers by absolute value, ascending
$order = SortOrder::custom(static fn (int|string $a, int|string $b): int => abs((int)$a) <=> abs((int)$b));
$list = SortedLinkedList::create([-3, 1, -2], $order);
$list->toArray(); // [1, -2, -3]
$list = SortedLinkedList::create([2, 1, 1, 3], SortOrder::ascending());
$list->insert(2); // [1, 1, 2, 2, 3]
$list->remove(2); // [1, 1, 2, 3] — first occurrence only
$list->removeAll(1); // [2, 3]
$list->insert(1); // [1, 2, 3]
$list->insert(1); // [1, 1, 2, 3]
$list->remove(1); // [1, 2, 3] — first occurrence only
$list = SortedLinkedList::create([1, 2, 3, 4, 5], SortOrder::ascending());
$evens = $list->filter(static fn (int|string $v): bool => (int)$v % 2 === 0);
$evens->toArray(); // [2, 4]
$other = SortedLinkedList::create([10, 20], SortOrder::ascending());
$merged = $list->merge($other);
$merged->toArray(); // [1, 2, 3, 4, 5, 10, 20]
$list = SortedLinkedList::create([1, 2, 3, 4, 5], SortOrder::ascending());
foreach ($list as $value) {
if ($value % 2 === 0) {
$list->remove($value); // safe — iterator skips removed nodes
}
}
$list->toArray(); // [1, 3, 5]