1. Go to this page and download the library: Download uniacid/sortedlinkedlist 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/ */
uniacid / sortedlinkedlist example snippets
use SortedLinkedList\IntegerSortedLinkedList;
$list = new IntegerSortedLinkedList();
$list->add(5);
$list->add(2);
$list->add(8);
$list->add(1);
// Elements are automatically sorted
foreach ($list as $value) {
echo $value . " "; // Output: 1 2 5 8
}
// Binary search for efficient lookups
$index = $list->binarySearch(5); // Returns index of element
// Bulk operations
$list->addAll([3, 7, 4]);
$filtered = $list->filter(fn($v) => $v > 3);
$doubled = $list->map(fn($v) => $v * 2);
use SortedLinkedList\IntegerSortedLinkedList;
use SortedLinkedList\StringSortedLinkedList;
use SortedLinkedList\FloatSortedLinkedList;
// Integer list
$intList = new IntegerSortedLinkedList();
$intList->add(42);
$intList->add(17);
$intList->add(99);
echo $intList->first(); // 17
echo $intList->last(); // 99
// String list
$stringList = new StringSortedLinkedList();
$stringList->addAll(['banana', 'apple', 'cherry']);
foreach ($stringList as $fruit) {
echo $fruit . ' '; // apple banana cherry
}
// Float list
$floatList = new FloatSortedLinkedList();
$floatList->addAll([3.14, 1.41, 2.71]);
echo $floatList->get(1); // 2.71 (second element after sorting)
$list = new IntegerSortedLinkedList();
$list->addAll(range(1, 1000));
// Binary search (fast for large lists)
$index = $list->binarySearch(500);
if ($index >= 0) {
echo "Found at index: " . $index;
}
// Contains check
if ($list->contains(750)) {
echo "List contains 750";
}
// Find first occurrence of value >= 400
$index = $list->binarySearchInsertPosition(400);
echo "Insert position for 400: " . $index;
$list = new IntegerSortedLinkedList();
$list->addAll([5, 2, 8, 1, 9, 3, 7]);
// Filter elements
$filtered = $list->filter(fn($v) => $v > 5);
// Result: [7, 8, 9]
// Map transformation
$doubled = $list->map(fn($v) => $v * 2);
// Result: [2, 4, 6, 10, 14, 16, 18]
// Reduce to single value
$sum = $list->reduce(fn($carry, $item) => $carry + $item, 0);
echo "Sum: " . $sum; // 35
// Slice operations
$slice = $list->slice(2, 3); // Get 3 elements starting from index 2
// Result: [3, 5, 7]
$list1 = new StringSortedLinkedList();
$list1->addAll(['apple', 'banana', 'cherry']);
$list2 = new StringSortedLinkedList();
$list2->addAll(['banana', 'date', 'elderberry']);
// Remove all elements that exist in list2
$list1->removeAll($list2);
// list1 now contains: ['apple', 'cherry']
// Retain only elements that exist in both
$list3 = new StringSortedLinkedList();
$list3->addAll(['apple', 'banana', 'cherry']);
$list3->retainAll(['banana', 'cherry', 'date']);
// list3 now contains: ['banana', 'cherry']
$list = new IntegerSortedLinkedList();
$list->addAll([30, 10, 20]);
// Array-like access
echo $list[0]; // 10 (first sorted element)
echo $list[1]; // 20
echo $list[2]; // 30
// Check if index exists
if (isset($list[1])) {
echo "Index 1 exists";
}
// Note: Setting values via array access maintains sort order
$list[3] = 15; // Adds 15 to the list in sorted position
$list = new FloatSortedLinkedList();
$list->addAll([3.14, 1.41, 2.71, 1.73]);
// Convert to array
$array = $list->toArray();
// Result: [1.41, 1.73, 2.71, 3.14]
// Get values only (same as toArray)
$values = $list->values();
// Check if empty
if (!$list->isEmpty()) {
echo "List has " . $list->size() . " elements";
}
// Clear all elements
$list->clear();
echo $list->isEmpty() ? "Empty" : "Not empty"; // Empty
use SortedLinkedList\ImmutableSortedLinkedList;
use SortedLinkedList\Comparator\DateComparator;
use SortedLinkedList\Comparator\ObjectComparator;
use SortedLinkedList\Comparator\ReverseComparator;
use SortedLinkedList\Comparator\ChainComparator;
// Date sorting
$dateComparator = new DateComparator('Y-m-d');
$dateList = new ImmutableSortedLinkedList($dateComparator);
$dateList = $dateList->addAll(['2024-03-15', '2024-01-10', '2024-02-20']);
// Object property sorting
$users = [
(object)['name' => 'Alice', 'age' => 30],
(object)['name' => 'Bob', 'age' => 25],
];
$ageComparator = new ObjectComparator('age');
$userList = new ImmutableSortedLinkedList($ageComparator);
$userList = $userList->addAll($users);
// Reverse order
$reverseNumeric = new ReverseComparator(new NumericComparator());
$reverseList = new ImmutableSortedLinkedList($reverseNumeric);
// Multiple criteria (sort by age, then name)
$chainedComparator = new ChainComparator([
new ObjectComparator('age'),
new ObjectComparator('name')
]);
use SortedLinkedList\ImmutableSortedLinkedList;
$original = new ImmutableSortedLinkedList(new NumericComparator());
$original = $original->addAll([1, 2, 3, 4, 5]);
// Each operation returns a new instance
$version1 = $original->add(6);
$version2 = $original->remove(3);
$version3 = $version1->filter(fn($v) => $v % 2 === 0);
// All versions are independent
echo $original->size(); // 5
echo $version1->size(); // 6
echo $version2->size(); // 4
echo $version3->size(); // 3 (only even numbers from version1)