PHP code example of adambenovic / shipmonk-sorted-linked-list

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

    

adambenovic / shipmonk-sorted-linked-list example snippets


use AdamBenovic\SortedLinkedList\SortedLinkedList;

// Just insert values -- the type is detected automatically
$list = new SortedLinkedList();
$list->insert(42);
$list->insert(7);
$list->insert(15);

$list->toArray(); // [7, 15, 42]
$list->first();   // 7
$list->last();    // 42

$list = new SortedLinkedList();
$list->insert(1);       // OK -- type locked to "integer"
$list->insert(2);       // OK
$list->insert('hello'); // throws TypeMismatchException

use AdamBenovic\SortedLinkedList\SortedLinkedList;
use AdamBenovic\SortedLinkedList\ValueType;

// Auto-detect type from first insert
$list = new SortedLinkedList();

// Pre-declare the type (rejects wrong-type values even before first insert)
$ints = new SortedLinkedList(ValueType::Integer);
$strings = new SortedLinkedList(ValueType::String);

// Factory method -- creates a pre-populated sorted list
$list = SortedLinkedList::of(3, 1, 4, 1, 5);       // [1, 1, 3, 4, 5]
$list = SortedLinkedList::of('cherry', 'apple');     // ['apple', 'cherry']

use AdamBenovic\SortedLinkedList\IntSortedLinkedList;
use AdamBenovic\SortedLinkedList\StringSortedLinkedList;

$ints = new IntSortedLinkedList();       // first() returns int
$strings = new StringSortedLinkedList(); // first() returns string

$list = SortedLinkedList::of(3, 1, 2);

count($list);             // 3
$list->size;              // 3 (read-only from outside)

foreach ($list as $value) {
    echo $value;          // 1, 2, 3
}

json_encode($list);       // [1,2,3]
echo $list;               // SortedLinkedList<integer>[1, 2, 3]

$list = SortedLinkedList::of(1, 2, 3, 4, 5);

$even = $list->filter(fn(int|string $v) => $v % 2 === 0);
$even->toArray(); // [2, 4]

$a = SortedLinkedList::of('apple', 'cherry');
$b = SortedLinkedList::of('banana', 'date');

$merged = $a->merge($b);
$merged->toArray(); // ['apple', 'banana', 'cherry', 'date']

$list = SortedLinkedList::of(5, 5, 5);

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