PHP code example of vondrasoft / sorted-linked-list

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

    

vondrasoft / sorted-linked-list example snippets


use Vondrasoft\DataStructures\SortedLinkedList;

$list = new SortedLinkedList();

$list->add(5);
$list->add(1);
$list->add(3);

count($list);        // 3
$list->toArray();    // [1, 3, 5]
$list->contains(3);  // true
$list->isEmpty();    // false
$list->first();      // 1
$list->last();       // 5

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

$list->remove(3);    // true  (removes first occurrence)
$list->removeAll(1); // 1     (returns number of removed elements)
$list->toArray();    // [5]

$list->clear();      // removes all elements, resets type lock
$list->toArray();    // []
count($list);        // 0

$list = new SortedLinkedList();
$list->add(1);
$list->add('hello'); // throws InvalidTypeException

use Vondrasoft\DataStructures\SortedLinkedList;

$list = new SortedLinkedList();

$list->add(5);
$list->add(1);
$list->add(3);

count($list);        // 3
$list->toArray();    // [1, 3, 5]
$list->contains(3);  // true
$list->isEmpty();    // false
$list->first();      // 1
$list->last();       // 5

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

$list->remove(3);    // true  (odebere první výskyt)
$list->removeAll(1); // 1     (vrátí počet odebraných prvků)
$list->toArray();    // [5]

$list->clear();      // odebere všechny prvky, uvolní zámek typu
$list->toArray();    // []
count($list);        // 0

$list = new SortedLinkedList();
$list->add(1);
$list->add('hello'); // vyhodí InvalidTypeException

src/
  SortedLinkedList.php              Main class (Iterator + Countable)
  Node.php                          Linked list node
  ValueType.php                     Enum for type locking
  Exception/InvalidTypeException.php
tests/
  SortedLinkedListTest.php          69 tests, 107 assertions
benchmarks/
  benchmark.php                     Performance benchmarks incl. array comparison

src/
  SortedLinkedList.php              Hlavní třída (Iterator + Countable)
  Node.php                          Uzel linked listu
  ValueType.php                     Enum pro zamykání typu
  Exception/InvalidTypeException.php
tests/
  SortedLinkedListTest.php          69 testů, 107 assertions
benchmarks/
  benchmark.php                     Výkonnostní benchmarky včetně porovnání s array