PHP code example of krooyh / sorted-linked-list

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

    

krooyh / sorted-linked-list example snippets




ortedLinkedList\SortedLinkedList;
use SortedLinkedList\Type;
use SortedLinkedList\SortDirection;

//you can create an instance of SortedLinkedList with an array
$sortedLinkedList = new SortedLinkedList(elements: [1, 8, 3, 5]);

//you can create an instance of SortedLinkedList with a type
$sortedLinkedList = new SortedLinkedList(type: Type::STRING);

//you can create an instance of SortedLinkedList with a type and direction
$sortedLinkedList = new SortedLinkedList(
    elements: [1, 8, 3, 5],
    type: Type::INTEGER,
    direction: SortDirection::DESC
);

//you can add an element to the list
$sortedLinkedList->add(8);

//you can remove an element from the list
$sortedLinkedList->remove(3);

//you can iterate over the list
foreach ($sortedLinkedList as $element) {
    echo $element;
}

//you can get the size of the list
$sortedLinkedList->count();

//this will throw an exception because of type mismatch
$sortedLinkedList = new SortedLinkedList(elements: [1, 8, 3, 5], type: Type::STRING);