PHP code example of webfiori / collections

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

    

webfiori / collections example snippets



use WebFiori\Collections\LinkedList;
use WebFiori\Collections\Stack;
use WebFiori\Collections\Queue;
use WebFiori\Collections\Vector;

// LinkedList — doubly-linked, iterable
$list = new LinkedList();
$list->add("A");
$list->add("B");
echo $list->get(0); // "A"

// Stack — LIFO
$stack = new Stack();
$stack->push("Bottom");
$stack->push("Top");
echo $stack->pop(); // "Top"

// Queue — FIFO
$queue = new Queue();
$queue->enqueue("First");
$queue->enqueue("Second");
echo $queue->dequeue(); // "First"

// Vector — O(1) index access, array bracket syntax
$vector = new Vector();
$vector[] = "Hello";
$vector[] = "World";
echo $vector[0]; // "Hello"
echo json_encode($vector); // ["Hello","World"]


use WebFiori\Collections\LinkedList;

// Create a new linked list
$list = new LinkedList();

// Add elements
$list->add("First");
$list->add("Second");
$list->add("Third");

// Access elements by index
echo $list->get(0); // "First"
echo $list->get(1); // "Second"

// Insert at specific position
$list->insert("Inserted", 1); // Insert at index 1

// Remove elements
$removed = $list->remove(0); // Remove first element
$removed = $list->removeElement("Second"); // Remove by value

// Check if element exists
if ($list->contains("Third")) {
    echo "Found Third!";
}

// Get element index
$index = $list->indexOf("Third");

// Iterate through the list
foreach ($list as $item) {
    echo $item . "\n";
}

// Convert to array
$array = $list->toArray();

// Sort the list (works with strings, numbers, and Comparable objects)
$list->insertionSort(); // Ascending
$list->insertionSort(false); // Descending

// Get list size
echo "Size: " . $list->size();

// Clear all elements
$list->clear();

// Create a list with maximum 5 elements
$limitedList = new LinkedList(5);

// This will return false if limit is reached
$success = $limitedList->add("Item");


use WebFiori\Collections\Stack;

// Create a new stack
$stack = new Stack();

// Push elements onto the stack
$stack->push("Bottom");
$stack->push("Middle");
$stack->push("Top");

// Peek at the top element without removing it
echo $stack->peek(); // "Top"

// Pop elements from the stack
$top = $stack->pop(); // "Top"
$middle = $stack->pop(); // "Middle"

// Check stack size
echo "Size: " . $stack->size();

// Convert to array
$array = $stack->toArray();

// You can also use add() method (alias for push)
$stack->add("New Top");

// Create a stack with maximum 10 elements
$limitedStack = new Stack(10);

// This will return false if limit is reached
$success = $limitedStack->push("Item");


use WebFiori\Collections\Queue;

// Create a new queue
$queue = new Queue();

// Enqueue elements
$queue->enqueue("First");
$queue->enqueue("Second");
$queue->enqueue("Third");

// Peek at the front element without removing it
echo $queue->peek(); // "First"

// Dequeue elements
$first = $queue->dequeue(); // "First"
$second = $queue->dequeue(); // "Second"

// Check queue size
echo "Size: " . $queue->size();

// Convert to array
$array = $queue->toArray();

// You can also use add() method (alias for enqueue)
$queue->add("Fourth");

// Create a queue with maximum 100 elements
$limitedQueue = new Queue(100);

// This will return false if limit is reached
$success = $limitedQueue->enqueue("Item");


use WebFiori\Collections\Vector;

// Create a new vector
$vector = new Vector();

// Add elements
$vector->add("First");
$vector->add("Second");

// O(1) index access
echo $vector->get(0); // "First"

// Set element at index
$vector->set(0, "Modified");

// Insert at position
$vector->insert("Middle", 1);

// Remove by index or value
$vector->removeAt(0);
$vector->remove("Middle");

// Array bracket syntax (ArrayAccess)
$vector[] = "New";
$vector[0] = "Replaced";
echo $vector[0]; // "Replaced"
isset($vector[0]); // true
unset($vector[0]);

// JSON serialization
echo json_encode($vector); // ["New"]

// Find elements
$index = $vector->indexOf("New"); // 0 or -1 if not found

// Replace
$vector->replace("New", "Newer");

// Iterate
foreach ($vector as $index => $value) {
    echo "$index: $value\n";
}


use WebFiori\Collections\Comparable;
use WebFiori\Collections\LinkedList;

class Person implements Comparable {
    private $name;
    private $age;
    
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
    
    public function compare($other): int {
        if (!($other instanceof Person)) {
            return 1;
        }
        
        if ($this->age == $other->age) {
            return 0;
        }
        
        return $this->age > $other->age ? 1 : -1;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function getAge() {
        return $this->age;
    }
}

// Usage
$list = new LinkedList();
$list->add(new Person("Alice", 30));
$list->add(new Person("Bob", 25));
$list->add(new Person("Charlie", 35));

// Sort by age
$list->insertionSort(); // Ascending by age

$data = ["key" => "value"];
$list = new LinkedList();
$list->add($data);

// Modify the original data
$data["key"] = "modified";

// The list contains the modified data
$retrieved = $list->get(0);
echo $retrieved["key"]; // "modified"