PHP code example of jast99 / sorted-linked-list

1. Go to this page and download the library: Download jast99/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/ */

    

jast99 / sorted-linked-list example snippets


use JaSt99\SortedLinkedList\SortedLinkedList;

$integers = SortedLinkedList::ofIntegers();
$strings = SortedLinkedList::ofStrings();

$list = SortedLinkedList::ofIntegers();
$list->insert(5);
$list->insert(1);
$list->insert(3);

$list->toArray(); // [1, 3, 5]

$list->remove(3);  // true
$list->remove(99); // false

$list->contains(5);  // true
$list->contains(99); // false

$list->first(); // smallest value
$list->last();  // largest value

$list->count();   // number of elements
$list->isEmpty(); // true if empty
count($list);     // works too (Countable interface)

foreach ($list as $value) {
    echo $value . PHP_EOL;
}

$list->toArray();        // [1, 3, 5]
$list->toString();       // '1, 3, 5'
$list->toString(';');    // '1;3;5'
$list->toString(' | ');  // '1 | 3 | 5'

// Case-insensitive string sorting
$list = SortedLinkedList::ofStrings(strcasecmp(...));
$list->insert('Banana');
$list->insert('apple');
$list->toArray(); // ['apple', 'Banana']

// Reverse integer sorting
$list = SortedLinkedList::ofIntegers(fn(int $a, int $b): int => $b <=> $a);
$list->insert(1);
$list->insert(3);
$list->toArray(); // [3, 1]

$list = SortedLinkedList::ofIntegers();
$list->insert('foo'); // throws ListTypeMismatchException