PHP code example of kariricode / data-structure

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

    

kariricode / data-structure example snippets


use KaririCode\DataStructure\Collection\ArrayList;

$list = new ArrayList();
$list->add("Item 1");
$list->add("Item 2");
echo $list->get(0); // Outputs: Item 1

use KaririCode\DataStructure\Collection\LinkedList;

$linkedList = new LinkedList();
$linkedList->add("First");
$linkedList->add("Second");
$linkedList->remove("First");

use KaririCode\DataStructure\Heap\BinaryHeap;

$heap = new BinaryHeap();
$heap->add(10);
$heap->add(5);
$heap->add(20);
echo $heap->poll(); // Outputs: 5 (min-heap by default)

use KaririCode\DataStructure\Map\HashMap;

$map = new HashMap();
$map->put("key1", "value1");
echo $map->get("key1"); // Outputs: value1

use KaririCode\DataStructure\Set\TreeSet;

$set = new TreeSet();
$set->add("value1");
$set->add("value2");
echo $set->contains("value1"); // Outputs: true

use KaririCode\DataStructure\Stack\ArrayStack;

$stack = new ArrayStack();
$stack->push("First");
$stack->push("Second");
echo $stack->peek(); // Outputs: Second
$stack->pop();       // Removes "Second"

use KaririCode\DataStructure\Queue\ArrayDeque;

$deque = new ArrayDeque();
$deque->addFirst("First");
$deque->addLast("Last");
echo $deque->peekLast(); // Outputs: Last
$deque->removeLast();    // Removes "Last"