PHP code example of martingold / linked-list

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

    

martingold / linked-list example snippets




use MartinGold\LinkedList\SortedLinkedList;

$list = new SortedLinkedList();

$list->insert(1);
$list->insert(3);
$list->insert(7);

var_dump($list->pop());


use MartinGold\LinkedList\Comparator\Comparator;
use MartinGold\LinkedList\SortedLinkedList;

class Product
{
    public function __construct(
        public readonly float $quantity, 
    ){
        //
    }
}

/**
 * @implements Comparator<Product>
 */
class ProductComparator implements Comparator
{
    /**
     * Returns 1 when $a value is greater than $b.
     * Returns 0 when $a value is same as $b value.
     * Returns -1 when $a value is lesser than $b.
     */
    public function compare(Product $a, Product $b): int
    {
        return $a->quantity <=> $b->quantity;
    }
}

$productList = new SortedLinkedList(new ProductComparator());
$productList->insert(new Product(20));
$productList->insert(new Product(31));
$productList->insert(new Product(12));

foreach ($productList as $product) {
    echo $product->quantity;
}

// 12
// 20
// 31


php >= 8.2